Can you help me with this, in C#
Given an Enum
public enum InterferenceEnum
{
None = 0,
StrongNoiseSource = 1,
MediumNoiseSource = 2,
VerySensitiveSignal = 4,
SensitiveSignal = 8,
}
and a Dynamic Enum from this
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;
}
using the function above
List<string> nets_List = new List<string>() { "A", "B", "C" };
netListEnum = GenerateEnumerations(nets_List, "netsenum");
Now if i have a variable with value "None", i can get the enum by
SomeEnum enum = (SomeEnum)Enum.Parse(typeof(SomeEnum), "EnumValue");
using the first Enum, i can get the enum of string "None"
InterferenceEnum enum = (InterferenceEnum)Enum.Parse(typeof(InterferenceEnum), "None");
How can i get the enum for the dynamic generated enum?
var enum = (netListEnum.GetType())Enum.Parse(typeof(netListEnum.GetType()), "None");
the above code is wrong because i still "casting" it with the netListEnum Type, here is the updated code
var enum = Enum.Parse(netListEnum, "None");