我想使用LINQ表达式来使用Expression.CompileToMethod方法在类中生成一个实例方法。不允许对这段代码使用reflection.emit,并且从来没有使用linq来处理除LCG方法之外的任何东西:(我需要访问类中的字段。如何使用LINQ表达式访问“this”?LINQ表达式和这个
Update : 似乎并不像它支持当我试图通过一个methodbuilder到CompileToMethod不是静态的,然后我得到一个参数异常有人可以证实,LINQ表达式只能用来创建静态方法
AssemblyName asmName = new AssemblyName("foo");
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
asmName,
AssemblyBuilderAccess.RunAndCollect);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("foo");
TypeBuilder typeBuilder = moduleBuilder.DefineType(
"foo",
TypeAttributes.Public,
typeof(object));
FieldInfo field = typeBuilder.DefineField("FooBar", typeof(string), FieldAttributes.Private);
ConstructorBuilder constructor = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { typeof(string) });
// want to use linq expressions for constructor too.
ILGenerator generator = constructor.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_1);
generator.Emit(OpCodes.Stfld, field);
generator.Emit(OpCodes.Ret);
MethodBuilder method = typeBuilder.DefineMethod("Bar", MethodAttributes.Public | MethodAttributes.Static);
Expression writeLine = Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string), typeof(object) }, null),
Expression.Constant("The value is {0}"),
Expression.Constant("no this")); //Expression.MakeMemberAccess("this", field)); // need to access this.
Expression.Lambda<Action>(
writeLine).CompileToMethod(method);
Type myType = typeBuilder.CreateType();
object instance = Activator.CreateInstance(myType, "FooBar");
MethodInfo barMethod = myType.GetMethod("Bar", BindingFlags.Static | BindingFlags.Public);
barMethod.Invoke(instance, null);
。?
更新2: 看起来不支持
private void CompileToMethodInternal(MethodBuilder method, DebugInfoGenerator debugInfoGenerator)
{
ContractUtils.RequiresNotNull(method, "method");
**ContractUtils.Requires(method.IsStatic, "method");**
请发布代码,以便我们可以更好地了解您正在尝试做什么。 – mayabelle
我想用linq编译一些能够访问“this”的代码,非常简单。不过我会写一个简单的例子。 – user1985513