C #. NET Dynamic Creation type and assembly in memory

Source: Internet
Author: User
Tags emit

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 );

}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.