I need to create a dynamic enumeration and then get the type using Type.GetType (). Is it possible?
In the code below, a dynamic enumeration and an attempt to use its qualified name will be created. This is fine if I store the assembly first (using AssemblyBuilderAccess.RunAndSave). However, this is not possible if I use only AssemblyBuilderAccess.Run; a BindingFailure error occurs; could not find assembly. My impression was that the Run option allowed me to create and use assemblies without actually storing (or have access to various builders).
(Note: the use of the Type.GetType () code below is not mine. I cannot change this code.)
How can I create a dynamic enumeration and reference it without saving the assembly?
private Type CreateType()
{
System.Reflection.Emit.AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new System.Reflection.AssemblyName("temporaryAssembly"), AssemblyBuilderAccess.Run);
System.Reflection.Emit.ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("temporaryAssembly");
System.Reflection.Emit.EnumBuilder enumBuilder = moduleBuilder.DefineEnum("Temp", System.Reflection.TypeAttributes.Public, typeof(int));
return enumBuilder.CreateType();
}
private void DoStuff()
{
Type t = CreateType();
Type createAnotherOfSameType = Type.GetType(t.AssemblyQualifiedName);
}/
source
share