C#.net dynamically created types and assemblies 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 TypeBuilder = null;
ModuleBuilder ModuleBuilder = null;
MethodBuilder MethodBuilder = null;
PropertyBuilder propertybuilder = null;
FieldBuilder fieldbuilder = null;
AssemblyBuilder AssemblyBuilder = null;
ILGenerator 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.ActiveRecordAttribute). 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.setcustomattribute (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.LDFLD, 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.STFLD, FieldBuilder);
Ilgenerator.emit (Opcodes.ret);
Propertybuilder.setsetmethod (MethodBuilder);
}
Create Class
return TypeBuilder.CreateType ();
Return Assemblybuilder.gettype (ClassName);
Return ModuleBuilder.GetType (ClassName);
}
}
}