This question already has an answer here: Cast int to enum in C# 21 answers Convert String to Type in C# [duplicate] 4 answers Enum String Name from Value 6 answers Get the name of Enum value 3 answers Get enum name when value is known 6 answers In addition to the name of enum itself you should know namespace and assembly that enum is declared at. Assuming you run code below in assem
这个问题在这里已经有了答案: 在int C中枚举21枚答案 将字符串转换为在C#中键入[复制] 4个答案 来自Value 6的Enum字符串名称的答案 获取Enum值3的答案的名称 当值已知6个答案时获取枚举名称 除了enum本身的名称之外,您应该知道enum声明的名称空间和程序集。 假设你在程序集中运行下面的代码,其中定义了枚举,并且所有的枚举都在编译时在同一个命名空间中定义,你可以这样做: string dataType = "Cheese"; int
This question already has an answer here: Cast int to enum in C# 21 answers Just cast the int to the enum . o.MyEnum = (CustomEnumProp) myInt; You can also use the Enum.IsDefined method to check that the int is valid. if (Enum.IsDefined(typeof(CustomEnumProp), myInt)) o.MyEnum = (CustomEnumProp) myInt;
这个问题在这里已经有了答案: 在int C中枚举21枚答案 只需将int转换为enum 。 o.MyEnum = (CustomEnumProp) myInt; 您也可以使用Enum.IsDefined方法来检查int是否有效。 if (Enum.IsDefined(typeof(CustomEnumProp), myInt)) o.MyEnum = (CustomEnumProp) myInt;
Possible Duplicate: Cast int to Enum in C# I fetch a int value from the database and want to cast the value to an enum variable. In 99.9% of the cases, the int will match one of the values in the enum declaration public enum eOrderType { Submitted = 1, Ordered = 2, InReview = 3, Sold = 4, ... } eOrderType orderType = (eOrderType) FetchIntFromDb(); In the edge case, the
可能重复: 在int中将int转换为Enum 我从数据库中获取一个int值,并希望将该值转换为一个枚举变量。 在99.9%的情况下,int将与枚举声明中的一个值匹配 public enum eOrderType { Submitted = 1, Ordered = 2, InReview = 3, Sold = 4, ... } eOrderType orderType = (eOrderType) FetchIntFromDb(); 在边缘情况下,值将不匹配(无论是数据损坏还是手动进入并混淆数据)。 我可以使用switch语句并
This question already has an answer here: Cast int to enum in C# 21 answers Enum ToString with user friendly strings 19 answers 你并不需要任何特别的东西,你可以将你从数据库中得到的整数转换为你的枚举: int valueFromDB = 4; Days enumValue = (Days)valueFromDB; I would not suggest this approach. You need a look-up table for days. For instance create table Days( DaysID INT PRIMARY KEY, Name
这个问题在这里已经有了答案: 在int C中枚举21枚答案 枚举ToString用户友好的字符串19答案 你并不需要任何特别的东西,你可以将你从数据库中得到的整数转换为你的枚举: int valueFromDB = 4; Days enumValue = (Days)valueFromDB; 我不会建议这种方法。 你需要一个查找表几天。 例如 create table Days( DaysID INT PRIMARY KEY, Name VARCHAR(20)) 所有其他表将有一个DaysID的外键列。 我之所以反对你的方法是因
This question already has an answer here: Cast int to enum in C# 21 answers 根据你的枚举声明,Suit在[0..3]范围内,而等级在[1..13]范围内(注意,该等级不是以0为基础的),所以应该纠正内循环: for (int rankVal = 0; rankVal < 13; rankVal++) // <- 14 changed for 13: [0..13] has the same length as [1..14] { cards[suitVal * 13 + rankVal] = new Card((Suits)suitVal, (Rank)(rankVal + 1));
这个问题在这里已经有了答案: 在int C中枚举21枚答案 根据你的枚举声明,Suit在[0..3]范围内,而等级在[1..13]范围内(注意,该等级不是以0为基础的),所以应该纠正内循环: for (int rankVal = 0; rankVal < 13; rankVal++) // <- 14 changed for 13: [0..13] has the same length as [1..14] { cards[suitVal * 13 + rankVal] = new Card((Suits)suitVal, (Rank)(rankVal + 1)); // <- removed -1 from
This question already has an answer here: Cast int to enum in C# 21 answers Just cast the integer to the enum: SpiceLevels level = (SpiceLevels) 3; and of course the other way around also works: int number = (int) SpiceLevels.Ferocious; See also MSDN: Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of enumeration el
这个问题在这里已经有了答案: 在int C中枚举21枚答案 只需将整数转换为枚举: SpiceLevels level = (SpiceLevels) 3; 当然也可以采用其他方式: int number = (int) SpiceLevels.Ferocious; 另请参阅MSDN: 每个枚举类型都有一个基础类型,可以是除char之外的任何整型。 枚举元素的默认基础类型是int。 ... 但是,显式转换对于从枚举类型转换为整型类型是必需的 enum SpiceLevels { None = 0, Mild = 1, Moderat
Possible Duplicate: Cast int to Enum in C# I got the following enum: public enum detallistaDocumentStatus { /// <remarks/> ORIGINAL, /// <remarks/> COPY, /// <remarks/> REEMPLAZA, /// <remarks/> DELETE, } then I got a class property of type detallistaDocumentStatus: public detallistaDocumentStatus documentStatus { get {
可能重复: 在int中将int转换为Enum 我得到了以下枚举: public enum detallistaDocumentStatus { /// <remarks/> ORIGINAL, /// <remarks/> COPY, /// <remarks/> REEMPLAZA, /// <remarks/> DELETE, } 然后我得到了类型为detallistaDocumentStatus的类属性: public detallistaDocumentStatus documentStatus { get { return this.d
Possible Duplicate: Cast int to enum in C# If I have the following code: enum foo : int { option1 = 1, option2, ... } private foo convertIntToFoo(int value) { // Convert int to respective Foo value or throw exception } What would the conversion code look like? It's fine just to cast your int to Foo: int i = 1; Foo f = (Foo)i; If you try to cast a value that's no
可能重复: 在int中枚举枚举在C#中枚举 如果我有以下代码: enum foo : int { option1 = 1, option2, ... } private foo convertIntToFoo(int value) { // Convert int to respective Foo value or throw exception } 转换代码是什么样的? 把你的int投给Foo很好: int i = 1; Foo f = (Foo)i; 如果您尝试转换未定义的值,它仍然有效。 这可能来自唯一的伤害,就是你以后如何使用价值。 如果你确
I'm trying to create a small Excel-Addin with just 1 Project in the Solution. I downloaded the Tools at http://social.msdn.microsoft.com/Forums/vstudio/en-US/0c144a87-917a-4eb1-8716-da61f7efdafc/office-developer-tools-for-visual-studio-2013-march-2014-update-now-available?forum=lightswitch and could create a Add-In. I can even debug it, but as soon as I rebuild the Solution, i get the 2 fo
我正在尝试在解决方案中创建一个只包含1个项目的小型Excel-Addin。 我下载了工具http://social.msdn.microsoft.com/Forums/vstudio/en-US/0c144a87-917a-4eb1-8716-da61f7efdafc/office-developer-tools-for-visual-studio-2013-march -2014-update-now-available?forum = lightswitch并且可以创建一个Add-In。 我甚至可以调试它,但只要我重建解决方案,我得到以下2个错误: 错误1“SignFile”任务不支持“TargetFrameworkVe
Trying to use bearer token based authentification in simple .Net Core Web API project. Here is my Startup.cs app.UseMvc(); //--- const string secretKey = "mysupersecret_secretkey!123"; SymmetricSecurityKey signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)); SigningCredentials signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256); //--- cons
尝试在简单的.Net Core Web API项目中使用基于持证者令牌的身份验证。 这是我的Startup.cs app.UseMvc(); //--- const string secretKey = "mysupersecret_secretkey!123"; SymmetricSecurityKey signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)); SigningCredentials signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256); //--- const string audience = "Aud