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.
typeof
on a type that doesn't exist at compile time? REALLY need to see some code here... – Cookerytypeof(T)
is being used within the generic method. – Outgrowtypeof()
is a static analysis thing. It doesn't exist as far as the compiler exists. If you want aType
, useyourAssembly.GetType(fullName)
– Cookery