[英]LINQ Expressions and this
我想使用LINQ表達式在使用Expression.CompileToMethod方法的類中生成實例方法。 不允許對這部分代碼使用reflection.emit,並且除了LCG方法之外,決不使用linq :(我需要訪問該類中的一個字段。如何使用LINQ表達式訪問“ this”?
更新:似乎不支持它。 當我嘗試將不是靜態的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");**
您可以只使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.