using System; using System.Reflection.Emit; public class MyProgram { public static void Main(string[] args) { DynamicMethod method = new DynamicMethod( "AddIntegers", typeof(int), new Type[] { typeof(int), typeof(int) } ); ILGenerator il = method.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Add); il.Emit(OpCodes.Ret); FuncaddIntegers = (Func ) method.CreateDelegate(typeof(Func )); Console.WriteLine(addIntegers(2, 3)); } }
using System; using System.Reflection.Emit; public class MyProgram { public static void Main(string[] args) { TypeBuilder typeBuilder = CreateDynamicType(); Type myType = typeBuilder.CreateType(); object obj = Activator.CreateInstance(myType); PropertyInfo propInfo = myType.GetProperty("MyProperty"); FuncThis example uses IEmitter to create a dynamic type called "MyType" with a private field and a public property called "MyProperty". The program then creates an instance of the type, sets the value of "MyProperty" to 42, and prints the property value to the console. The package library for IEmitter is System.Reflection.Emit.