你能用C#
帮我解决这个问题鉴于Enum
public enum InterferenceEnum
{
None = 0,
StrongNoiseSource = 1,
MediumNoiseSource = 2,
VerySensitiveSignal = 4,
SensitiveSignal = 8,
}
和来自此
的动态枚举public Type GenerateEnumerations(List<string> lEnumItems, string assemblyName)
{
// Create Base Assembly Objects
AppDomain appDomain = AppDomain.CurrentDomain;
AssemblyName asmName = new AssemblyName(assemblyName);
AssemblyBuilder asmBuilder = appDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run);
// Create Module and Enumeration Builder Objects
ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule(assemblyName + "_module");
EnumBuilder enumBuilder = modBuilder.DefineEnum(assemblyName, TypeAttributes.Public, typeof(int));
enumBuilder.DefineLiteral("None", 0);
int flagCnt = 1;
foreach (string fmtObj in lEnumItems)
{
enumBuilder.DefineLiteral(fmtObj, flagCnt);
flagCnt++;
}
var retEnumType = enumBuilder.CreateType();
//asmBuilder.Save(asmName.Name + ".dll");
return retEnumType;
}
使用上面的功能
List<string> nets_List = new List<string>() { "A", "B", "C" };
netListEnum = GenerateEnumerations(nets_List, "netsenum");
现在,如果我有一个带有值的变量&#34;无&#34;,我可以通过
获得枚举SomeEnum enum = (SomeEnum)Enum.Parse(typeof(SomeEnum), "EnumValue");
使用第一个枚举,我可以得到字符串的枚举&#34;无&#34;
InterferenceEnum enum = (InterferenceEnum)Enum.Parse(typeof(InterferenceEnum), "None");
如何获取动态生成的枚举的枚举?
var enum = (netListEnum.GetType())Enum.Parse(typeof(netListEnum.GetType()), "None");
上面的代码是错误的,因为我仍然&#34; cast&#34;它与netListEnum类型,这是更新的代码
var enum = Enum.Parse(netListEnum, "None");
答案 0 :(得分:1)
你已经拥有它 - “Enum.Parse()”会将指定类型的枚举boxed返回给对象。但是盒装对象是你创建的枚举类型;如果你在其上调用“GetType()”,它将返回相同的类型:
List<string> nets_List = new List<string>() { "A", "B", "C" };
var netListEnumType = GenerateEnumerations(nets_List, "netsenum");
var typeName = netListEnumType.Name; // returns "netsenum"
var typeTypeName = netListEnumType.GetType().Name; // returns "RuntimeType", the actual name of the instantiated Type class.
foreach (var enumName in nets_List)
{
var enumValBoxed = Enum.Parse(netListEnumType, enumName);
Console.WriteLine(enumValBoxed.ToString()); // Writes "A", "B" and "C"
Debug.Assert(enumValBoxed.GetType() == netListEnumType); // no assert yay.
}
我看到你的代码唯一的问题是你正在做netListEnum.GetType()
但是netListEnum已经是Type
类型 - 实际上你创建的类型 - 所以这不是必需的。< / p>
如果需要将创建的枚举传递给通用对象中的某个泛型方法,例如一个Dictionary<string, TEnum>
,你可以通过MakeGenericMethod
答案 1 :(得分:0)
我的Enum制造者:
public class XEnum
{
private EnumBuilder enumBuilder;
private int index;
private AssemblyBuilder _ab;
private AssemblyName _name;
public XEnum(string enumname)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
_name = new AssemblyName("MyAssembly");
_ab = currentDomain.DefineDynamicAssembly(
_name, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder mb = _ab.DefineDynamicModule("MyModule");
enumBuilder = mb.DefineEnum(enumname, TypeAttributes.Public, typeof(int));
}
/// <summary>
/// adding one string to enum
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public FieldBuilder add(string s)
{
FieldBuilder f = enumBuilder.DefineLiteral(s, index);
index++;
return f;
}
/// <summary>
/// adding array to enum
/// </summary>
/// <param name="s"></param>
public void addRange(string[] s)
{
for (int i = 0; i < s.Length; i++)
{
enumBuilder.DefineLiteral(s[i], i);
}
}
/// <summary>
/// getting index 0
/// </summary>
/// <returns></returns>
public object getEnum()
{
Type finished = enumBuilder.CreateType();
_ab.Save(_name.Name + ".dll");
Object o1 = Enum.Parse(finished, "0");
return o1;
}
public Type getType()
{
Type finished = enumBuilder.CreateType();
_ab.Save(_name.Name + ".dll");
return finished;
}
/// <summary>
/// getting with index
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public object getEnum(int i)
{
Type finished = enumBuilder.CreateType();
_ab.Save(_name.Name + ".dll");
Object o1 = Enum.Parse(finished, i.ToString());
return o1;
}
}
通过字符串数组进行枚举:
public static object STE(string[] @enum)
{
if (@enum.Length > 0)
{
XEnum xe = new XEnum("Enum");
xe.addRange(@enum);
return xe.getEnum();
}
else return null;
}
从枚举中获取选定的字符串:
public static object STE(string sel, string[] @enum)
{
XEnum xe = new XEnum("Enum");
xe.addRange(@enum);
var obj= xe.getType();
return Enum.Parse(obj, sel);
}
要使用STE将它们放入静态类中, 所以用这个:
string[] ab = {"a", "b"};
object abEnum = STE(ab); //creates Enum
private object aEnum = STE("a", ab); //gets selected Value