我想知道是否可以获取枚举值的属性而不是枚举本身的属性?例如,假设我有以下枚举:
using System.ComponentModel; // for DescriptionAttribute
enum FunkyAttributesEnum
{
[Description("Name With Spaces1")]
NameWithoutSpaces1,
[Description("Name With Spaces2")]
NameWithoutSpaces2
}
Run Code Online (Sandbox Code Playgroud)
我想要的是枚举类型,产生2元组的枚举字符串值及其描述.
价值很容易:
Array values = System.Enum.GetValues(typeof(FunkyAttributesEnum));
foreach (int value in values)
Tuple.Value = Enum.GetName(typeof(FunkyAttributesEnum), value);
Run Code Online (Sandbox Code Playgroud)
但是如何获取描述属性的值,以填充Tuple.Desc?如果属性属于枚举本身,我可以想到如何做到这一点,但我不知道如何从枚举的值中获取它.
我有以下内容 enum
public enum myEnum
{
ThisNameWorks,
This Name doesn't work
Neither.does.this;
}
Run Code Online (Sandbox Code Playgroud)
enum带有"友好名字"的s 不可能吗?
我想在我的枚举上设置空间.这是我的代码示例:
public enum category
{
goodBoy=1,
BadBoy
}
Run Code Online (Sandbox Code Playgroud)
我想设置
public enum category
{
Good Boy=1,
Bad Boy
}
Run Code Online (Sandbox Code Playgroud)
当我检索时,我想从枚举中看到Good Boy的结果
这是我的Model Class,我们有一个Type可以是Zombie或Human
public class User
{
public int ID { get; set; }
public string Name { get; set; }
public Type Type { get; set; }
public List<Wepon> WeposInList { get; set; }
}
public enum Type
{ [Description("Zombie")] Zombie,
[Description("Human")] Human
}
Run Code Online (Sandbox Code Playgroud)
目前它正在Int中保存数据
我想将数据保存为Human和Zombie,而不是int
我正在寻找一个关于如何创建类似枚举类的最佳实践,而不是数字包含字符串值.像这样的东西:
public static class CustomerType
{
public static string Type1 = "Customer Type 1";
public static string Type2 = "Customer Type 2";
}
Run Code Online (Sandbox Code Playgroud)
我会在整个应用程序中使用此类作为我需要CustomerType的所有情况的值.我不能使用Enum,因为这是遗留系统,这样的值在任何地方都是硬编码的,我只是想把它们集中在一个地方.
在上面的示例中,问题是我应该用于声明变量:
设置这些类和值的最佳做法是什么?
我有一个枚举如下:
public enum MyEnum { One, Two, Three}
Run Code Online (Sandbox Code Playgroud)
我想将一些字符串削减到枚举之上,例如,下面的字符串将被解析为 MyEnum.Two:
"Two", "TWO", "Second", "2"
Run Code Online (Sandbox Code Playgroud)
我知道我可以维护一个映射函数来完成这项工作。但是,我只是想找到一个更好的方法,例如,覆盖 Enum.Parse 函数,或者类似的东西。我曾尝试使用 IConvertable,但似乎不可能。任何的想法?
有没有办法使用整数索引从枚举返回适当的值?例如,如果有枚举颜色{红色,绿色,蓝色)是否有一个函数,值0将返回红色,1将返回绿色,2将返回蓝色?
我想知道是否有一种方法可以检查该值是否是枚举的描述。我知道
Enum.IsDefined(typeof(EnumEntity),value)
Run Code Online (Sandbox Code Playgroud)
可以用来检查值是否在枚举中,但是描述怎么样?
例如,
public enum LicenseTypes
{
[Description("A License")]
A,
[Description("B License")]
B,
[Description("C License")]
C
}
Run Code Online (Sandbox Code Playgroud)
有没有办法检查“A License”是枚举 LicenseTypes 的描述?
如何在C#中将枚举值显示为V1.0,V2.0,V3.0?
enum value
{
V1.0,
V2.0,
V3.0
}
Run Code Online (Sandbox Code Playgroud) 如何将 C# 类中的属性设置为仅允许具有两个字符串之一?
例如,考虑这个类:
class Route
{
private string color;
}
Run Code Online (Sandbox Code Playgroud)
如果我希望颜色只允许为“深红色”或“浅蓝色”,我该怎么做?
我尝试定义一个枚举,如下所示:
public enum Color
{
"Dark red",
"Light blue"
}
Run Code Online (Sandbox Code Playgroud)
然后使用它而不是使用内置的字符串类,但这似乎不是正确的方法。
我需要使用其中一个值= 50/50制作枚举
这不可能吗?Visual Studio似乎不喜欢它.
我可能不得不让这个字段成为数据库查找.
请告诉我.
如何使用包含空格的枚举项名称?
enum Coolness
{
Not So Cool = 1,
VeryCool = 2,
Supercool = 3
}
Run Code Online (Sandbox Code Playgroud)
我通过下面的代码获取Enum项目名称
string enumText = ((Coolness)1).ToString()
Run Code Online (Sandbox Code Playgroud)
我不会改变这段代码,但上面的代码应该返回Not So Cool.有没有使用oops概念来实现这一目标?在这里,我不想更改检索语句.
我想填充一个提供枚举值和描述的列表。我如何在 C# 中做到这一点?
我知道如何创建值列表,但我也想包含描述。这是我的枚举的样子:
using System.ComponentModel;
public enum BusinessCategory
{
[Description("Computers & Internet")]
ComputersInternet = 1,
[Description("Finance & Banking")]
FinanceBanking = 2,
[Description("Healthcare")]
Healthcare = 3,
[Description("Manufacturing")]
Manufacturing = 4
}
Run Code Online (Sandbox Code Playgroud)
我希望我的列表看起来像:
[
{ 1, "Computers & Internet" },
{ 2, "Finance & Banking" },
{ 3, "Healthcare" },
{ 4, "Manufacturing" }
]
Run Code Online (Sandbox Code Playgroud)