I am creating a dynamic assembly using Reflection.Emit, which includes one class. I have an error that throws a BadImageException. To solve this problem, I need to see the compiled code, and therefore I save the dynamic assembly to disk.
I have already tried PEVerify against a build that seems to think there are no errors. Now I want to view the generated code in Reflector, but the assembly looks empty (that I don’t know).
Any idea why this is happening?
var assemblyName = new AssemblyName("An.Assembly");
var appDomain = Thread.GetDomain();
var assemblyBuilder = appDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
var typeBuilder = moduleBuilder.DefineType("MyClass", TypeAttributes.Public | TypeAttributes.Class);
...
typeBuilder.CreateType();
assemblyBuilder.Save("temp.dll");
By the way, I already use a plug-in Relection.Emitfor a reflector which does not help with this problem.