C #. NET Dynamic Creation type and assembly in memory
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. reflection;
Using system. reflection. emit;
Namespace com. frzeng. Arch. Common. Data. ar
{
Public class typecreator
{
Public static type creator (string classname, int propertiescount)
{
Idictionary <string, type> properties = new dictionary <string, type> ();
Type T = typeof (string );
Properties. Add (New keyvaluepair <string, type> ("ID", typeof (INT )));
For (INT I = 0; I <propertiescount; I ++)
{
Properties. Add (New keyvaluepair <string, type> ("FF" + I, T ));
}
Return creator (classname, properties );
}
Public static type creator (string classname, idictionary <string, type> Properties)
{
Appdomain currentdomain = system. Threading. thread. getdomain (); // appdomain. currentdomain;
Typebuilder = NULL;
Modulebuilder = NULL;
Methodbuilder = NULL;
Propertybuilder = NULL;
Fieldbuilder = NULL;
Assemblybuilder = NULL;
Ilgenerator = NULL;
Customattributebuilder cab = NULL;
Methodattributes methodattrs;
// Define a dynamic assembly
Assemblybuilder = currentdomain. definedynamicassembly (New assemblyname ("Test2"), assemblybuilderaccess. Run); // assemblybuilder. getcallingassembly (). fullname
// Define a dynamic module
Modulebuilder = assemblybuilder. definedynamicmodule ("modulename", true );
// Define a runtime class with specified name and attributes.
Typebuilder = modulebuilder. definetype (classname, typeattributes. Public | typeattributes. Class | typeattributes. beforefieldinit | typeattributes. serializable );
Cab = new customattributebuilder (typeof (Castle. activerecord. activerecordattritor). getconstructor (type. emptytypes), new object [0]);
Typebuilder. setcustomattribute (CAB );//
Cab = new customattributebuilder (typeof (Castle. activerecord. propertyattribute). getconstructor (type. emptytypes), new object [0]);
Methodattrs = methodattributes. Public | methodattributes. specialname | methodattributes. hidebysig;
Foreach (keyvaluepair <string, type> kV in properties)
{
// Add the class variable, such as "m_stripaddress"
Fieldbuilder = typebuilder. definefield ("field _" + kv. Key, Kv. Value, fieldattributes. Public );
Propertybuilder = typebuilder. defineproperty (Kv. Key, system. reflection. propertyattributes. hasdefault, Kv. Value, null );
If (Kv. Key! = "ID ")
Propertybuilder. setcustomattribute (CAB );//
Else
Propertybuilder. setcustomattribuilder (New customattributebuilder (typeof (Castle. activerecord. primarykeyattribute). getconstructor (type. emptytypes), new object [0]); //
Methodbuilder = typebuilder. definemethod ("Get _" + kv. Key, methodattrs, Kv. Value, type. emptytypes );
Ilgenerator = methodbuilder. getilgenerator ();
Ilgenerator. emit (Opcodes. ldarg_0 );
Ilgenerator. emit (Opcodes. lddes, fieldbuilder );
Ilgenerator. emit (Opcodes. Ret );
Propertybuilder. setgetmethod (methodbuilder );
Methodbuilder = typebuilder. definemethod ("SET _" + kv. Key, methodattrs, typeof (void), new type [] {kV. Value });
Ilgenerator = methodbuilder. getilgenerator ();
Ilgenerator. emit (Opcodes. ldarg_0 );
Ilgenerator. emit (Opcodes. ldarg_1 );
Ilgenerator. emit (Opcodes. stdes, fieldbuilder );
Ilgenerator. emit (Opcodes. Ret );
Propertybuilder. setsetmethod (methodbuilder );
}
// Create class
Return typebuilder. createtype ();
Return assemblybuilder. GetType (classname );
Return modulebuilder. GetType (classname );
}
}
}