Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I have written the following

C#
namespace SimpleDynamic
{
    class Program
    {
        public class SimpleClass
        {
         public string Name;
         public int Age;

        }



        static void Main(string[] args)
        {
         AppDomain appDomain = AppDomain.CurrentDomain;
         AssemblyName aName = new AssemblyName("SimpleDyn");
         AssemblyBuilder assemBuilder = appDomain.DefineDynamicAssembly(aName,
           AssemblyBuilderAccess.Save, @"c:\assemblies");
         ModuleBuilder modBuilder = assemBuilder.DefineDynamicModule("SimpleDyn.dll",
           "SimpleDyn.dll");


         TypeBuilder tb = modBuilder.DefineType("SimpleDyn.SimpleClass",
            TypeAttributes.Public | TypeAttributes.BeforeFieldInit, typeof(object));

         FieldBuilder field1 = tb.DefineField("Name", typeof(string), FieldAttributes.Public);
         FieldBuilder field2 = tb.DefineField("Age", typeof(int), FieldAttributes.Public);

         tb.CreateType();
         assemBuilder.Save("SimpleDyn.dll");


        }
    }
}

however using ildasm when I compare the manifest of both SimpleClass (static and dynamic )they are totally different, it seems that the dynamic type manifest has been cut off,besides when I reference statically the generated dll from other project, dynamic SimpleClass does not show its members.
please can someone help me?
Posted
Comments
Sergey Alexandrovich Kryukov 8-Jan-16 16:17pm    
I did not try it, but this is a good question (up-voted). Don't you think that the problem is the assembly saved in c:\assemblies and then in your working directory? They are two different files. Which one lacks the manifest? The background is: most assemblies have single module. In general case, you create several modules, and one of all of them has assembly manifest. The notion of "assembly" is: this is the set of the modules with one referencing other modules (not in the same sense as assembly reference), and only one of those modules has the assembly manifest. If you want a single-module assembly, you have to create only one file. The assembly and its only module is one single file.
—SA
Member 11763476 8-Jan-16 16:38pm    
thank you Sergey:I think that to generate a single module assembly you have to use the same file name when creating the ModuleBuilder ,second parameter ,that when you call AssemblyBuilder.Save and provide an assembly filename,SimpleDyn.dll in this case.I have also tried to use other overloaded appDomain.DefineDynamicAssembly without passing a string dir, in this case the dll is created in the application's base directory and the problem persists.
Sergey Alexandrovich Kryukov 8-Jan-16 16:53pm    
I thank that, too, but it needs some testing. Your problem is saving twice. Why? Which one misses the manifest? The example in MSDN is very clear.
—SA
Member 11763476 8-Jan-16 17:04pm    
the dynamic version is the one cut off,I don't understand why you say I'm saving twice,as I see it the dynamic dll is saved only when Save is called,
I think that surely you right but I've tried a lot of options, wich include saving in the applications directory an using different name patterns and I cant find the right one

Here it is one attempt more, this time following Dr. Dobbs naming pattern,
now I realize what Sergei could mean when saying saving twice,the first statically defined Simpleclass, and the second dynamically defined SimpleClass are different,because they live in different namespaces,SimpleDynamic and SimpleDyn respectively,in the previous snippets was not clear cause the top namespace declaration was not visible,however in this attempt I have renamed the dynamically built class to SimpleClass1,the purpose of building these two identical classes side by side is to compare them using ildasm.





C#
namespace SimpleDynamic
{
    class Program
    {
        public class SimpleClass
        {
         public string Name;
         public int Age;

        }



        static void Main(string[] args)
        {
         AppDomain appDomain = AppDomain.CurrentDomain;
         AssemblyName aName = new AssemblyName();
            aName.Name = "SimpleReflection";
         AssemblyBuilder assemBuilder = appDomain.DefineDynamicAssembly(aName,
           AssemblyBuilderAccess.Save);
         ModuleBuilder modBuilder = assemBuilder.DefineDynamicModule(aName.Name,
           "SimpleDyn.dll");


         TypeBuilder tb = modBuilder.DefineType("SimpleDyn.SimpleClass1",
            TypeAttributes.Public | TypeAttributes.BeforeFieldInit, typeof(object));

         FieldBuilder field1 = tb.DefineField("Name", typeof(string), FieldAttributes.Public);
         FieldBuilder field2 = tb.DefineField("Age", typeof(int), FieldAttributes.Public);

         tb.CreateType();
         assemBuilder.Save("SimpleDyn.dll");


        }
    }
 
Share this answer
 
Please see my comment to the question.
The code sample in this MSDN article explains it all: AssemblyBuilder Class (System.Reflection.Emit)[^].

See also Generating Code at Run Time With Reflection.Emit | Dr Dobb's[^].

Note that you need to save the assembly only once, when the module is created, as well as the code in the module. In this approach, you should have only one file on output, at the very end.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900