C# 可能有枚举的字符串吗?
我想要一个枚举,如中所示:C# 可能有枚举的字符串吗?,c#,.net,enums,C#,.net,Enums,我想要一个枚举,如中所示: enum FilterType { Rigid = "Rigid", SoftGlow = "Soft / Glow", Ghost = "Ghost", } 如何做到这一点?有更好的方法吗?它将用于一个对象的实例,在那里它将被序列化/反序列化。它还将填充一个下拉列表。这是不可能的。C#只允许整数枚举类型(int、short、long等)。您可以创建一个轻量级的“类枚举”类,也可以使用静态常量 static class FilterTypes {
enum FilterType
{
Rigid = "Rigid",
SoftGlow = "Soft / Glow",
Ghost = "Ghost",
}
如何做到这一点?有更好的方法吗?它将用于一个对象的实例,在那里它将被序列化/反序列化。它还将填充一个下拉列表。这是不可能的。C#只允许整数枚举类型(int、short、long等)。您可以创建一个轻量级的“类枚举”类,也可以使用静态常量
static class FilterTypes
{
public const string Rigid = "Rigid";
// ...
}
// or ...
class FilterType
{
static readonly FilterType RigidFilterType = new FilterType("Rigid");
string name;
FilterType(string name) // private constructor
{
this.name = name;
}
public static FilterType Rigid
{
get { return FilterType.RigidFilterType; }
}
// ...
}
不,不可能
已批准的枚举类型包括
字节,sbyte,short,ushort,int,uint,
龙,或者说乌龙
但是,您可以使用enum类检索已声明的enum名称:
string name = Enum.GetName(typeof(FilterType), FilterType.Rigid);
如果这对您不起作用,则在类中收集的字符串常量集合可能会失败。枚举始终链接到整数值。所以没有。
您可以执行FilterType.Rigid.ToString()来获取字符串值,尽管它不能直接本地化。否,但是如果您希望作用域“const”字符串并将其用作枚举,我将执行以下操作:
public static class FilterType
{
public const string Rigid = "Rigid";
public const string SoftGlow = "Soft / Glow";
public const string Ghost ="Ghost";
}
可以将枚举名称作为如下字符串获取
FilterType myType = FilterType.Rigid;
String strType = myType.ToString();
public static String GetEnumerationDescription(Enum e)
{
Type type = e.GetType();
FieldInfo fieldInfo = type.GetField(e.ToString());
DescriptionAttribute[] da = (DescriptionAttribute[])(fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false));
if (da.Length > 0)
{
return da[0].Description;
}
return e.ToString();
}
但是,您可能会被驼峰式/匈牙利式符号所困扰,但是您可以使用这样的方法轻松地将其转换为更为用户友好的字符串(这不是最漂亮的解决方案,我非常感谢您提供的优化信息):
公共共享函数NormalizeCamelCase(ByVal str作为字符串)作为字符串
如果String.IsNullOrEmpty(str),则
返回字符串。空
如果结束
尺寸i为整数=0
整数形式的Dim upperCount=0
将otherCount设置为整数=0
Dim normalizedString作为String=str
而我0并且也没有规范化字符串(i-1).Equals(““c”),那么
''当前字符不是第一个字符,前面的字符不是空格
''…插入空格,移动到下一个字符
normalizedString=normalizedString.Insert(i,“”)
i+=1
如果结束
如果不是Char.isleter(normalizedString,i),则
其他计数+=1
如果结束
''移动到下一个字符
i+=1
结束时
如果upperCount+otherCount=str.Length,则
''字符串在所有大写字母中,返回原始字符串
返回str
其他的
返回规范化字符串
如果结束
端函数
如果这还不够漂亮,您可能需要查看自定义属性,可以使用反射检索这些属性
using System.ComponentModel;
enum FilterType
{
[Description("Rigid")]
Rigid,
[Description("Soft / Glow")]
SoftGlow,
[Description("Ghost")]
Ghost ,
}
你可以这样算出值
FilterType myType = FilterType.Rigid;
String strType = myType.ToString();
public static String GetEnumerationDescription(Enum e)
{
Type type = e.GetType();
FieldInfo fieldInfo = type.GetField(e.ToString());
DescriptionAttribute[] da = (DescriptionAttribute[])(fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false));
if (da.Length > 0)
{
return da[0].Description;
}
return e.ToString();
}
不,但你可以这样作弊:
public enum FilterType{
Rigid,
SoftGlow,
Ghost
}
然后,当您需要它们的字符串值时,只需执行
FilterType.Rigid.ToString()
[System.ComponentModel.Description("Rigid")]
例如:
enum FilterType
{
[System.ComponentModel.Description("Rigid")]
Rigid,
[System.ComponentModel.Description("Soft / Glow")]
SoftGlow,
[System.ComponentModel.Description("Ghost")]
Ghost
}
并使用反射来获取描述
枚举扩展方法:
public static string GetDescription(this Enum en)
{
var type = en.GetType();
var memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
return ((DescriptionAttribute)attrs[0]).Description;
}
return en.ToString();
}
使用它:
FilterType = FilterType.Rigid;
string description= result.GetDescription();
在System.ComponentModel命名空间中,有一个类DescriptionAttribute在这里运行良好
enum FilterType
{
Rigid,
[Description("Soft / Glow")]
SoftGlow,
Ghost,
}
然后获取描述并故障转移到ToString()
var descriptionAttribute=Value.GetType()
.GetField(Value.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute),false)
.第()类
.FirstOrDefault()??新的DescriptionAttribute(Value.ToString());
如果您对扩展方法感到满意,您可以轻松地完成所需的工作:
//Can return string constants, the results of a Database call,
//or anything else you need to do to get the correct value
//(for localization, for example)
public static string EnumValue(this MyEnum e) {
switch (e) {
case MyEnum.First:
return "First Friendly Value";
case MyEnum.Second:
return "Second Friendly Value";
case MyEnum.Third:
return "Third Friendly Value";
}
return "Horrible Failure!!";
}
通过这种方式,您可以:
Private MyEnum value = MyEnum.First;
Console.WriteLine(value.EnumValue());
按照Shaun Bowe的例子,您也可以在C#3中使用enum的扩展方法来实现这一点(我没有想到,而且我自己也记不起我在哪里做过)
创建一个属性:
public class DisplayTextAttribute : Attribute {
public DisplayTextAttribute(String text) {
Text = text;
}
public string Text { get; set; }
}
创建扩展名:
public static class EnumHelpers {
public static string GetDisplayText(this Enum enumValue) {
var type = enumValue.GetType();
MemberInfo[] memberInfo = type.GetMember(enumValue.ToString());
if (memberInfo == null || memberInfo.Length == 0)
return enumValue.ToString();
object[] attributes = memberInfo[0].GetCustomAttributes(typeof(DisplayTextAttribute), false);
if (attributes == null || attributes.Length == 0)
return enumValue.ToString();
return ((DisplayTextAttribute)attributes[0]).Text;
}
}
我发现这是一个很好的解决方案。
在枚举中添加以下内容:
enum FilterType{
Rigid,
[DisplayText("Soft / Glow")]
SoftGlow,
Ghost
}
然后,您可以访问FilterType.GetDisplayText()
,这将收回未分配枚举的字符串和具有属性的枚举的displayText。这完全不可能。但是,您可以使用数组来伪造它
首先,以常规枚举的方式创建枚举:
public enum Regexs
{
ALPHA = 0,
NUMERIC = 1,
etc..
}
然后创建一个数组,该数组保存与枚举值对应的值:
private static string[] regexs = new string[]
{
"[A-Za-z]",
"[0-9]",
"etc"...
}
然后可以通过枚举值访问字符串:
public void runit(Regexs myregexEnum)
{
Regex regex = new Regex( regexs [(int)myregexEnum] );
}
这样做(我也为此感到内疚)会显示没有空格/特殊字符的用户文本(SoftGlow而不是“Soft/Glow”),我确信他希望能够做到这一点。有趣的方法。请确保不要在“高性能”路径中使用此代码,因为执行反射调用的成本相当高。这也是我过去所做的,在大多数情况下都很有效。这种方法的缺点是我看不到如何自动填充下拉列表。只需做一点工作,您至少可以选择迭代枚举的值。或者您可以定义一个静态字符串values[]array.DV,用于…中的NO“否,但如果您想作用于”const“strings…”这是一种很好的方法。您甚至可以将此作为所有枚举的扩展方法。谢谢。。我在当前项目中使用了很多静态方法来处理这样的枚举。但我从未想过使用扩展方法。我必须指出,通过在[System.Xml.Serialization.XmlEnum(“刚性”)]…[Description(“刚性”)]刚性等上定义一个属性来序列化它是一种很好的做法。我不认为这个解决方案对已经(两年半)被接受的答案有任何好处,因为如果对枚举进行任何更改,现在必须保持两个位置同步。