Created
March 25, 2013 19:54
-
-
Save k0stya/5240109 to your computer and use it in GitHub Desktop.
Generate assembly at runtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using System.Reflection.Emit; | |
namespace ConsoleApplication1 | |
{ | |
public class AssemblyGenerator | |
{ | |
public static ModuleBuilder CreateModuleBuilder(Action<ModuleBuilder> buildTypes, string assemblyName) | |
{ | |
AppDomain currentDomain = AppDomain.CurrentDomain; | |
AssemblyName assembName = new AssemblyName(assemblyName); | |
AssemblyBuilder ab = currentDomain.DefineDynamicAssembly( | |
assembName, AssemblyBuilderAccess.RunAndSave); | |
ModuleBuilder mb = ab.DefineDynamicModule(assembName.Name, assembName.Name + ".dll"); | |
buildTypes(mb); | |
ab.Save(assembName.Name + ".dll"); | |
return mb; | |
} | |
public static Type CreateEnum(ModuleBuilder mb, string enumName, params string[] values) | |
{ | |
EnumBuilder eb = mb.DefineEnum(enumName, TypeAttributes.Public, typeof(int)); | |
for (int i = 0; i < values.Length; i++) | |
{ | |
eb.DefineLiteral(values[i], i); | |
} | |
Type finished = eb.CreateType(); | |
return finished; | |
} | |
public static Type CreateClass(ModuleBuilder mb, string className, params string[] props) | |
{ | |
TypeBuilder typeBuilder = mb.DefineType(className, TypeAttributes.Public); | |
for (int i = 0; i < props.Length; i++) | |
{ | |
FieldBuilder nameBldr = typeBuilder.DefineField(props[i].ToLower() + i, | |
typeof(string), | |
FieldAttributes.Private); | |
var namePropBldr = typeBuilder.DefineProperty(props[i], PropertyAttributes.HasDefault, typeof(string), null); | |
MethodAttributes getSetAttr = | |
MethodAttributes.Public | MethodAttributes.SpecialName | | |
MethodAttributes.HideBySig; | |
MethodBuilder nameGetPropMthdBldr = | |
typeBuilder.DefineMethod("get_" + props[i] + i, | |
getSetAttr, | |
typeof(string), | |
Type.EmptyTypes); | |
ILGenerator custNameGetIL = nameGetPropMthdBldr.GetILGenerator(); | |
custNameGetIL.Emit(OpCodes.Ldarg_0); | |
custNameGetIL.Emit(OpCodes.Ldfld, nameBldr); | |
custNameGetIL.Emit(OpCodes.Ret); | |
MethodBuilder nameSetPropMthdBldr = | |
typeBuilder.DefineMethod("set_" + props[i] + i, | |
getSetAttr, | |
null, | |
new Type[] { typeof(string) }); | |
ILGenerator custNameSetIL = nameSetPropMthdBldr.GetILGenerator(); | |
custNameSetIL.Emit(OpCodes.Ldarg_0); | |
custNameSetIL.Emit(OpCodes.Ldarg_1); | |
custNameSetIL.Emit(OpCodes.Stfld, nameBldr); | |
custNameSetIL.Emit(OpCodes.Ret); | |
namePropBldr.SetGetMethod(nameGetPropMthdBldr); | |
namePropBldr.SetSetMethod(nameSetPropMthdBldr); | |
} | |
Type finished = typeBuilder.CreateType(); | |
return finished; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment