字符串重新枚举(为EString)的presentation? [英] string representation of an enum (estring)?
问题描述
我需要一个枚举或类似的东西做一些事情是这样的:
i need an enum or something similiar to do something like this:
公开枚举MyStringEnum {
[的StringValue(富A)]富=A,
[的StringValue(富B)]富=B}
public enum MyStringEnum { [StringValue("Foo A")] Foo = "A", [StringValue("Foo B")] Foo = "B" }
这可能吗?我的榜样,我回到回数据集psented作为转口货值为$ P $是A,B,C,D,E ..我需要一个解决方案,以恢复这个作为一个字符串再presentation?
is this possible? my example, i return back a dataset with a value represented as either A,B,C,D,E .. i need a solution to return this as a string representation?
我猜明显的是创建一个扩展方法或一些东西,刚在switch语句,并返回一个字符串..任何其他清洁解决方案?
i guess the obvious would be to create an extension method or something which just had a switch statement in and return a string .. any other cleaner solutions?
问候,
戴夫
推荐答案
难道是一个选项不使用枚举和使用结构呢?
Would it be an option not to use enum and use structs instead?
struct FooEnum
{
private int value;
private string name;
private FooEnum(int value, string name)
{
this.name = name;
this.value = value;
}
public static readonly FooEnum A = new FooEnum(0, "Foo A");
public static readonly FooEnum B = new FooEnum(1, "Foo B");
public static readonly FooEnum C = new FooEnum(2, "Foo C");
public static readonly FooEnum D = new FooEnum(3, "Foo D");
public override string ToString()
{
return this.name;
}
//TODO explicit conversion to int etc.
}
您可以再使用FooEnum喜欢用自己的ToString()重载枚举:
You could then use FooEnum like an enum with an own ToString() overload:
FooEnum foo = FooEnum.A;
string s = foo.ToString(); //"Foo A"
这篇关于字符串重新枚举(为EString)的presentation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!