除了乔恩的回答,您可以提供的GUID作为一个单独的类的字段,其中字段具有相同的名称及其相应的枚举值。
这可以用于填充字典快速查找后:
using System;
using System.Collections.Generic;
namespace SO2856896
{
enum DictionaryType
{
RegionType,
Nationality
}
class Consts
{
public class DictionaryTypeId
{
public static Guid RegionType = new Guid("21EC2020-3AEA-1069-A2DD-08002B30309D");
public static Guid Nationality = new Guid("21EC2020-3AEA-1069-A2DD-08002B30309E");
}
}
class Program
{
static void Main(string[] args)
{
Dictionary<DictionaryType, Guid> table = new Dictionary<DictionaryType, Guid>();
Type idType = typeof(Consts.DictionaryTypeId);
foreach (DictionaryType dicType in Enum.GetValues(
typeof(DictionaryType)))
{
System.Reflection.FieldInfo field = idType
.GetField(dicType.ToString(),
System.Reflection.BindingFlags.Static
| System.Reflection.BindingFlags.Public);
Guid guid = (Guid)field.GetValue(null);
table[dicType] = guid;
}
foreach (DictionaryType dicType in Enum.GetValues(
typeof(DictionaryType)))
{
Console.Out.WriteLine(dicType + ": " + table[dicType]);
}
}
}
}
输出:
RegionType: 21ec2020-3aea-1069-a2dd-08002b30309d
Nationality: 21ec2020-3aea-1069-a2dd-08002b30309e
我不完全知道我会选择我自己,但也许一个的组合乔恩的回答是一本字典,用来查看上面的指导和反思来填充它。
是的,Id属性是一个不错的主意,但我不能将它分配给枚举选项:( 枚举是在数据模型库中,不应该与实现(Guids)结合 – 2010-05-18 11:59:16
更改代码以反映单独的类 – 2010-05-18 12:08:16
谢谢你你的时间和精力! – 2010-05-18 12:28:47