Problem to save assembly via System.Reflection.Emit.
-
Hi I've used from this link to define a new simple type and then save to disk with .dll extension via this code :
private void button1_Click(object sender, EventArgs e)
{
AssemblyName asmName = new AssemblyName();
asmName.Name = "HelloWorld";AssemblyBuilder asmBuilder = Thread.GetDomain().DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule("HelloWorld"); TypeBuilder typeBuilder = modBuilder.DefineType("Hello", TypeAttributes.Public, typeof(object), new Type\[\] { typeof(IHello) }); MethodBuilder methodBuilder = typeBuilder.DefineMethod("SayHello", MethodAttributes.Private | MethodAttributes.Virtual, typeof(void), new Type\[\] { typeof(string) }); typeBuilder.DefineMethodOverride(methodBuilder, typeof(IHello).GetMethod("SayHello")); ILGenerator il = methodBuilder.GetILGenerator(); // string.Format("Hello, {0} World!", toWhom) il.Emit(OpCodes.Ldstr, "Hello, {0} World!"); il.Emit(OpCodes.Ldarg\_1); il.Emit(OpCodes.Call, typeof(string).GetMethod("Format", new Type\[\] { typeof(string), typeof(object) })); // Console.WriteLine("Hello, World!"); il.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type\[\] { typeof(string) })); il.Emit(OpCodes.Ret); Type type = typeBuilder.CreateType(); IHello hello = (IHello)Activator.CreateInstance(type); hello.SayHello("Emit"); asmBuilder.Save(asmName.Name + ".dll");
}
it saved my dll on disk, but when i open it via Reflector, it does not have anything! just have a module named 'RefEmit_OnDiskManifestModule' can anybody help me where is my problem and how to solve it ? thanks in advance
-
Hi I've used from this link to define a new simple type and then save to disk with .dll extension via this code :
private void button1_Click(object sender, EventArgs e)
{
AssemblyName asmName = new AssemblyName();
asmName.Name = "HelloWorld";AssemblyBuilder asmBuilder = Thread.GetDomain().DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule("HelloWorld"); TypeBuilder typeBuilder = modBuilder.DefineType("Hello", TypeAttributes.Public, typeof(object), new Type\[\] { typeof(IHello) }); MethodBuilder methodBuilder = typeBuilder.DefineMethod("SayHello", MethodAttributes.Private | MethodAttributes.Virtual, typeof(void), new Type\[\] { typeof(string) }); typeBuilder.DefineMethodOverride(methodBuilder, typeof(IHello).GetMethod("SayHello")); ILGenerator il = methodBuilder.GetILGenerator(); // string.Format("Hello, {0} World!", toWhom) il.Emit(OpCodes.Ldstr, "Hello, {0} World!"); il.Emit(OpCodes.Ldarg\_1); il.Emit(OpCodes.Call, typeof(string).GetMethod("Format", new Type\[\] { typeof(string), typeof(object) })); // Console.WriteLine("Hello, World!"); il.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type\[\] { typeof(string) })); il.Emit(OpCodes.Ret); Type type = typeBuilder.CreateType(); IHello hello = (IHello)Activator.CreateInstance(type); hello.SayHello("Emit"); asmBuilder.Save(asmName.Name + ".dll");
}
it saved my dll on disk, but when i open it via Reflector, it does not have anything! just have a module named 'RefEmit_OnDiskManifestModule' can anybody help me where is my problem and how to solve it ? thanks in advance
Hi, IMO the problem is with the
DefineDynamicModule()
, where you should use an overload and specify the DLL file there too. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.