When using the typeof
operator on type created through TypeBuilder, the operator will return null.
I'm curious why this happens and how to prevent it.
I'm starting to think this is a VS bug in the immediate window, but I'm not quite sure. It's very easy to blame others first.
Ok... code to reproduce issue:
static void Main()
{
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName("MyAssembly"),
AssemblyBuilderAccess.RunAndSave);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public, typeof(ArrayList));
ArrayList o = (ArrayList)Activator.CreateInstance(typeBuilder.CreateType());
Console.WriteLine(o.GetType().Name);
}
If you put a breakpoint after the variable o
and type typeof(MyType)
in the VS Immediate Windows you'll get the issue.