C# 我的枚举可以有友好名称吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1415140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can my enums have friendly names?
提问by JL.
I have the following enum
我有以下 enum
public enum myEnum
{
ThisNameWorks,
This Name doesn't work
Neither.does.this;
}
Is it not possible to have enum
s with "friendly names"?
enum
s不能带有“友好名称”吗?
采纳答案by RaYell
Enum value names must follow the same naming rules as all identifiers in C#, therefore only first name is correct.
枚举值名称必须遵循与 C# 中所有标识符相同的命名规则,因此只有名字是正确的。
回答by TimothyP
They follow the same naming rules as variable names. Therefore they should not contain spaces.
它们遵循与变量名称相同的命名规则。因此它们不应包含空格。
Also what you are suggesting would be very bad practice anyway.
此外,无论如何,您的建议都是非常糟糕的做法。
回答by Robban
Enum names live under the same rules as normal variable names, i.e. no spaces or dots in the middle of the names... I still consider the first one to be rather friendly though...
枚举名称与普通变量名称遵循相同的规则,即名称中间没有空格或点......我仍然认为第一个是相当友好的......
回答by aJ.
public enum myEnum
{
ThisNameWorks,
This_Name_can_be_used_instead,
}
回答by Yuriy Faktorovich
No, but you can use the DescriptionAttributeto accomplish what you're looking for.
不,但您可以使用DescriptionAttribute来完成您要查找的内容。
回答by MRG
You can use the Description
attribute to get that friendly name. You can use the code below:
您可以使用该Description
属性来获取该友好名称。您可以使用以下代码:
public static string ToStringEnums(Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
return ((DescriptionAttribute)attrs[0]).Description;
}
return en.ToString();
}
An example of when you would want to use this method: When your enum value is EncryptionProviderType
and you want enumVar.Tostring()
to return "Encryption Provider Type".
您希望何时使用此方法的示例:当您的枚举值为EncryptionProviderType
并且您希望enumVar.Tostring()
返回“加密提供程序类型”时。
Prerequisite: All enum members should be applied with the attribute [Description("String to be returned by Tostring()")]
.
先决条件:所有枚举成员都应使用属性[Description("String to be returned by Tostring()")]
。
Example enum:
示例枚举:
enum ExampleEnum
{
[Description("One is one")]
ValueOne = 1,
[Description("Two is two")]
ValueTow = 2
}
And in your class, you would use it like this:
在你的课堂上,你会像这样使用它:
ExampleEnum enumVar = ExampleEnum.ValueOne;
Console.WriteLine(ToStringEnums(enumVar));
回答by Beatles1692
I suppose that you want to show your enum values to the user, therefore, you want them to have some friendly name.
我想您想向用户显示您的枚举值,因此,您希望它们具有一些友好的名称。
Here's my suggestion:
这是我的建议:
Use an enum type pattern. Although it takes some effort to implement, it is really worth it.
使用枚举类型模式。尽管实施起来需要一些努力,但这确实是值得的。
public class MyEnum
{
public static readonly MyEnum Enum1=new MyEnum("This will work",1);
public static readonly MyEnum Enum2=new MyEnum("This.will.work.either",2);
public static readonly MyEnum[] All=new []{Enum1,Enum2};
private MyEnum(string name,int value)
{
Name=name;
Value=value;
}
public string Name{get;set;}
public int Value{get;set;}
public override string ToString()
{
return Name;
}
}
回答by Thomas Levesque
You could use the Description
attribute, as Yuriy suggested. The following extension method makes it easy to get the description for a given value of the enum:
Description
正如 Yuriy 建议的那样,您可以使用该属性。以下扩展方法可以轻松获取给定枚举值的描述:
public static string GetDescription(this Enum value)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name != null)
{
FieldInfo field = type.GetField(name);
if (field != null)
{
DescriptionAttribute attr =
Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attr != null)
{
return attr.Description;
}
}
}
return null;
}
You can use it like this:
你可以这样使用它:
public enum MyEnum
{
[Description("Description for Foo")]
Foo,
[Description("Description for Bar")]
Bar
}
MyEnum x = MyEnum.Foo;
string description = x.GetDescription();
回答by Mark Carpenter
If you have the following enum:
如果您有以下枚举:
public enum MyEnum {
First,
Second,
Third
}
You can declare Extension Methods for MyEnum
(like you can for any other type). I just whipped this up:
您可以声明扩展方法MyEnum
(就像您可以为任何其他类型一样)。我刚刚提出了这个:
namespace Extension {
public static class ExtensionMethods {
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!!";
}
}
}
With this Extension Method, the following is now legal:
使用此扩展方法,以下内容现在是合法的:
Console.WriteLine(MyEnum.First.EnumValue());
Hope this helps!!
希望这可以帮助!!
回答by Trainee4Life
One problem with this trick is that description attribute cannot be localized. I do like a technique by Sacha Barber where he creates his own version of Description attribute which would pick up values from the corresponding resource manager.
这个技巧的一个问题是描述属性不能被本地化。我确实喜欢 Sacha Barber 的一项技术,他创建了自己的 Description 属性版本,该版本将从相应的资源管理器中获取值。
http://www.codeproject.com/KB/WPF/FriendlyEnums.aspx
http://www.codeproject.com/KB/WPF/FriendlyEnums.aspx
Although the article is around a problem that's generally faced by WPF developers when binding to enums, you can jump directly to the part where he creates the LocalizableDescriptionAttribute.
尽管本文围绕的是 WPF 开发人员在绑定到枚举时通常面临的一个问题,但您可以直接跳转到他创建 LocalizableDescriptionAttribute 的部分。