In pre-order, you can also achieve this by using it in the System.Reflection.Emit namespace, and I find it possible to provide a method implementation using LINQ expression trees. Which type is neat:
var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("TestAssembly"), AssemblyBuilderAccess.RunAndSave); var mod = assembly.DefineDynamicModule("TestModule"); var type = mod.DefineType("TestType"); var method = type.DefineMethod("Increment", MethodAttributes.Public, typeof(int), Type.EmptyTypes); Expression<Func<int, int>> inc = (a) => a + 1;
It might seem a bit complicated at first, but it is really cool stuff, and you let the compiler generate hard stuff - an implementation of the method. But you really cannot create fields that require some IL. This is a lot of fun, but very tiring work.
RENOUNCEMENT:
I did not try to run the above code. But I know that this is something like that, some time has passed since I did something like that.
John leidegren
source share