matt Gomm

matt Gomm

  • NA
  • 18
  • 774

create or generate automatically struct

Jul 30 2019 3:00 AM
Hello everyone,
 
I am actually trying to create or generate automatically struct using the class "TypeBuilder".
 
I have already found a solution to create an "enum".
 
Here is the Code that I found as example on the microsoft-page:
  1. using System.Reflection;  
  2. using System.Reflection.Emit;  
  3.   
  4. namespace testnamespace  
  5. {  
  6.    class test  
  7.    {  
  8.       public void CreateEnum()  
  9.       {  
  10.          // Get the current application domain for the current thread.  
  11.          AppDomain currentDomain = AppDomain.CurrentDomain;  
  12.         
  13.       // Create a dynamic assembly in the current application domain,   
  14.       // and allow it to be executed and saved to disk.  
  15.       AssemblyName aName = new AssemblyName("TempAssembly");  
  16.       AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(  
  17.       aName, AssemblyBuilderAccess.RunAndSave);  
  18.         
  19.       // Define a dynamic module in "TempAssembly" assembly. For a single-  
  20.       // module assembly, the module has the same name as the assembly.  
  21.       ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");  
  22.    
  23.       // Define a public enumeration with the name "Elevation" and an   
  24.       // underlying type of Integer.  
  25.       EnumBuilder eb = mb.DefineEnum("Elevation", TypeAttributes.Public, typeof(int));  
  26.         
  27.       // Define two members, "High" and "Low".  
  28.       eb.DefineLiteral("Low", 0);  
  29.       eb.DefineLiteral("High", 1);  
  30.        
  31.        // Create the type and save the assembly.  
  32.       Type finished = eb.CreateType();  
  33.       ab.Save(aName.Name + ".dll");  
  34.       foreach(object o in Enum.GetValues(finished) )  
  35.       {  
  36.          Console.WriteLine("{0}.{1} = {2}", finished, o, ((int) o));  
  37.       }  
  38. }  
How can I do the same procedure to get a struct (NOT A CLASS ;) )
 
Thank you.

Answers (4)