vendredi 15 juillet 2016

Expression.Lamda is not working with dynamically generated class

Here is sample code do test:

var assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("asm"), AssemblyBuilderAccess.Run);
var builder = assembly.DefineDynamicModule("MainModule");
Type type = builder.DefineType("newType");
var parameter = Expression.Parameter(type);
Console.WriteLine(type);
var expr = Expression.Lambda(Expression.Constant(1), parameter);

Here I get an exception

Specified method is not supported

How can I avoid it? I don't have this type an compile-time, and I want to create a constructor by using Expressions instead of Emitting it manually. Is it even possible? I did it with instance methods, but I did it without using this. Now I need it, but access to type if forbidden while it is under construction.





Aucun commentaire:

Enregistrer un commentaire