返回类型不是字符串的枚举?
由于枚举使用整数,我可以使用什么其他结构来对链接到名称的值进行类似枚举的访问:
[我知道这是错误的,正在寻找替代方案]
private enum Project
{
Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1"),
Maintenance = new Guid("39D31D4-28EC-4832-827B-A11129EB2"),
Upgrade = new Guid("892F865-E38D-46D7-809A-49510111C1"),
Sales = new Guid("A5690E7-1111-4AFB-B44D-1DF3AD66D435"),
Replacement = new Guid("11E5CBA2-EDDE-4ECA-BDFD-63BDBA725C8C"),
Modem = new Guid("6F686C73-504B-111-9A0B-850C26FDB25F"),
Audit = new Guid("30558C7-66D9-4189-9BD9-2B87D11190"),
Queries = new Guid("9985242-516A-4151-B7DD-851112F562")
}
编辑2014-07-20
这是对此的更新答案问题。 使用带有辅助方法的 Attribute 类,定义枚举所需的额外属性。
public enum MultiValueEnum
{
[FooAttribute("alpha", 20d, true)]
First,
[FooAttribute("beta", 40.91d, false)]
Second,
[FooAttribute("gamma", 1.2d, false)]
Third,
}
public class FooAttribute : Attribute
{
internal FooAttribute(string name, double percentage, bool isGood)
{
this.Name = name;
this.Percentage = (decimal)percentage;
this.IsGood = isGood;
}
public string Name { get; private set; }
public decimal Percentage { get; private set; }
public bool IsGood { get; private set; }
}
public static TAttribute GetAttribute<TAttribute>(this Enum value)
where TAttribute : Attribute
{
var type = value.GetType();
var name = Enum.GetName(type, value);
return type.GetField(name)
.GetCustomAttributes(false)
.OfType<TAttribute>()
.SingleOrDefault();
}
这使得事情变得如此简单:
MultiValueEnum enumVar = MultiValueEnum.First;
var enumStringValue = enumVar.GetAttribute<FooAttribute>().Name;
var enumValueDecimal = enumVar.GetAttribute<FooAttribute>().Percentage;
var enumBool = enumVar.GetAttribute<FooAttribute>().IsGood;
Since enumeration uses integers, what other structure can I use to give me enum-like access to the value linked to the name:
[I know this is wrong, looking for alternative]
private enum Project
{
Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1"),
Maintenance = new Guid("39D31D4-28EC-4832-827B-A11129EB2"),
Upgrade = new Guid("892F865-E38D-46D7-809A-49510111C1"),
Sales = new Guid("A5690E7-1111-4AFB-B44D-1DF3AD66D435"),
Replacement = new Guid("11E5CBA2-EDDE-4ECA-BDFD-63BDBA725C8C"),
Modem = new Guid("6F686C73-504B-111-9A0B-850C26FDB25F"),
Audit = new Guid("30558C7-66D9-4189-9BD9-2B87D11190"),
Queries = new Guid("9985242-516A-4151-B7DD-851112F562")
}
EDIT 2014-07-20
This is a newer answer to this question. Using the Attribute class with a helper method, define the extra attributes needed on your enum.
public enum MultiValueEnum
{
[FooAttribute("alpha", 20d, true)]
First,
[FooAttribute("beta", 40.91d, false)]
Second,
[FooAttribute("gamma", 1.2d, false)]
Third,
}
public class FooAttribute : Attribute
{
internal FooAttribute(string name, double percentage, bool isGood)
{
this.Name = name;
this.Percentage = (decimal)percentage;
this.IsGood = isGood;
}
public string Name { get; private set; }
public decimal Percentage { get; private set; }
public bool IsGood { get; private set; }
}
public static TAttribute GetAttribute<TAttribute>(this Enum value)
where TAttribute : Attribute
{
var type = value.GetType();
var name = Enum.GetName(type, value);
return type.GetField(name)
.GetCustomAttributes(false)
.OfType<TAttribute>()
.SingleOrDefault();
}
Which makes it this easy:
MultiValueEnum enumVar = MultiValueEnum.First;
var enumStringValue = enumVar.GetAttribute<FooAttribute>().Name;
var enumValueDecimal = enumVar.GetAttribute<FooAttribute>().Percentage;
var enumBool = enumVar.GetAttribute<FooAttribute>().IsGood;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
否则,您可以为枚举创建一个自定义属性,它可以保存 Guid。
沿着这些线的东西:
然后你会像这样使用它:
最后你可以(我总是这样做)创建一个扩展来轻松获取guid:
所以最后你对枚举所要做的就是:
我使用这个将描述附加到枚举的方法,通常是包含空格的较长字符串,因此不能用作名称。
Otherwise you could create a custom Attribute for your enum, which can hold the Guid.
Something alongside these lines:
And you'd then use it like so:
And finally you could (I always do this) create an extension for easily getting the guid:
So in the end all you have to with your enums is:
I use this approach to attach descriptions to enums, typically longer strings containing spaces, which thus cannot be used as names.
我已经看到 SubSonic 使用这个方法(结构)来存储列和表名称。
编辑:- 感谢您对代码缺陷的评论。 首先,如果 Guid 字符串不是无效的,它将编译。 至于不创建实例来访问变量,是的,它们需要是公共静态
I've seen this method (struct) used by SubSonic to store Column and Table names.
EDIT:- Thanks for commenting on deficiencies in code. In first place it will compile if the Guid strings are not invalid. As for not create instances to access variables yes they need to be public static
我可能会在这方面走字典路线。 基本上有一个查找表:
I would probably go the dictionary route on this one. Have a lookup table basically:
如果您需要正确的类似于枚举的语义和类型安全,那么您可以使用这样的模式。
(如果您需要转换运算符、
GetUnderlyingType
、ToString
等额外功能,您可以进一步充实它。如果您想为多个enum< 重复使用该模式/code> 类具有不同的底层类型,那么您可以将任何通用代码移动到通用的抽象基类中。)
If you need proper
enum
-like semantics and type-safety then you can use a pattern like this.(You could flesh it out further if you require extras like conversion operators,
GetUnderlyingType
,ToString
etc. If you wanted to re-use the pattern for multipleenum
-like classes with different underlying types then you could move any common code into a generic, abstract base class.)当遇到这种问题时,我使用带有 const 的结构作为公共成员:
When confronted with this kind of problem I used structs with consts as public members:
您可以创建一个仅包含常量值的静态类。
例如:
这样,类就简单地充当容器,并且不能从中创建对象。
在 VB 中,这将是一个模块:
You could create a static class that just contains constant values.
For example:
This way the class acts simply as a container and object cannot be created from it.
In VB this would be a Module:
枚举类型仅支持整数类型(char 除外)作为其值。 但是,您可以使用字典之类的东西来查找名称和值。
另一种选择是在静态类中包含一系列只读值。
The enum type can only support the integral types (except char) as its value. You could however use something like a Dictionary to do lookups of a a name to a value.
Another alternative is to have a series of readonly values in a static class.