2 using System
.Reflection
;
3 using System
.Reflection
.Emit
;
7 public static int Main()
9 AssemblyName assemblyName
= new AssemblyName ();
10 assemblyName
.Name
= "TestAssembly";
11 AssemblyBuilder assemblyBuilder
= AppDomain
.CurrentDomain
.DefineDynamicAssembly (assemblyName
, AssemblyBuilderAccess
.Run
);
13 ModuleBuilder moduleBuilder
= assemblyBuilder
.DefineDynamicModule ("TestModule");
14 TypeBuilder typeBuilder
= moduleBuilder
.DefineType ("TestType", TypeAttributes
.Public
);
16 FieldBuilder fieldBuilder
= typeBuilder
.DefineField ("TestField",
18 FieldAttributes
.Private
);
21 PropertyBuilder propertyBuilder
= typeBuilder
.DefineProperty ("TestProperty",
22 PropertyAttributes
.HasDefault
,
24 new Type
[] { typeof (int) }
);
26 MethodBuilder getMethodBuilder
= typeBuilder
.DefineMethod ("TestGetMethod",
27 MethodAttributes
.Public
,
32 ILGenerator IL
= getMethodBuilder
.GetILGenerator();
34 IL
.Emit (OpCodes
.Ldarg_0
);
35 IL
.Emit (OpCodes
.Ldfld
, fieldBuilder
);
36 IL
.Emit (OpCodes
.Ret
);
39 MethodBuilder setMethodBuilder
= typeBuilder
.DefineMethod ("TestSetMethod",
40 MethodAttributes
.Public
,
42 new Type
[] { typeof(int) }
);
43 IL
= setMethodBuilder
.GetILGenerator();
45 IL
.Emit (OpCodes
.Ldarg_0
);
46 IL
.Emit (OpCodes
.Ldarg_1
);
47 IL
.Emit (OpCodes
.Stfld
, fieldBuilder
);
48 IL
.Emit (OpCodes
.Ret
);
50 propertyBuilder
.SetGetMethod (getMethodBuilder
);
51 propertyBuilder
.SetSetMethod (setMethodBuilder
);
53 typeBuilder
.CreateType ();
55 Type type
= moduleBuilder
.GetType ("TestType", true);
57 PropertyInfo propertyInfo
= type
.GetProperty ("TestProperty");