Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem to save assembly via System.Reflection.Emit.

Problem to save assembly via System.Reflection.Emit.

Scheduled Pinned Locked Moved C#
helpdotnettutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hdv212
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • H hdv212

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups