gpt4 book ai didi

c# - LINQ 表达式和这个

转载 作者:行者123 更新时间:2023-11-30 13:06:24 26 4
gpt4 key购买 nike

我想使用 LINQ 表达式在使用 Expression.CompileToMethod 方法的类中生成实例方法。不允许对这段代码使用 reflection.emit,并且除了 LCG 方法之外,从未将 linq 用于任何事情:(我需要访问类中的一个字段。如何使用 LINQ 表达式访问“this”?

更新:似乎不支持它。当我尝试将一个方法构建器传递给非静态的 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");**

最佳答案

你可以只使用Expression.Constant:

var thisExpr = Expression.Constant(this);
var fieldExpr = Expression.Field(thisExpr, "fieldName");

例如

public class ExprTest
{
private readonly int value;
public ExprTest(int value)
{
this.value = value;
}

public Func<int> GetValueExpr()
{
var fieldExpr = Expression.Field(Expression.Constant(this), "value");
var lambda = Expression.Lambda<Func<int>>(fieldExpr);
return lambda.Compile();
}
}

var e = new ExprTest(5);
int i = e.GetValueExpr()(); //i == 5

关于c# - LINQ 表达式和这个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19414811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com