getvalueexpre-6ren">
gpt4 book ai didi

c# - Expression> 到 Expression> "Getter"到 "Setter"

转载 作者:太空狗 更新时间:2023-10-29 17:42:25 26 4
gpt4 key购买 nike

我是表达式的新手,我想知道如何以任何方式转换我的表达式

假设在这个例子中我的 TModel 是 Customer 类型,并像这样将它分配到某个地方:

Expression<Func<TModel, string>> getvalueexpression = customer =>customer.Name

类似于

Expression<Action<TModel,string>> setvalueexpression = [PSEUDOCODE] getvalueexpression = input
Action<TModel,string> Setter = setvalueexpression.Compile();
Setter(mycustomer,value);

简而言之,我想以某种方式构建和编译一个表达式,将我的 getter 表达式指定的客户名称设置为特定值。

最佳答案

修改后的版本。这个类可能比你能在周围找到的许多其他类更好 :-) 这是因为这个版本支持直接属性(p => p.B)(和其他人一样 :-))、嵌套属性(p => p.B.C.D),字段(“终端”和“中间”,所以在 p => p.B.C.DBD 可以是字段)和类型的“内部”转换(因此 p => ((BType)p.B).C.Dp => (p.B as BType)。 C.D)。唯一不支持的是“终端”元素的转换(因此没有 p => (object)p.B)。

生成器中有两个“代码路径”:简单表达式 (p => p.B) 和“嵌套”表达式。 .NET 4.0 有代码变体(具有 Expression.Assign 表达式类型)。从我的一些基准测试中,最快的委托(delegate)是:“简单”Delegate.CreateDelegate 属性,Expression.Assign 字段和“简单”FieldSetter对于字段(这比字段的 Expression.Assign 慢一点)。所以在 .NET 4.0 下你应该去掉所有标记为 3.5 的代码。

部分代码不是我的。初始(简单)版本基于 Fluent NHibernate 代码(但它仅支持直接属性),其他一些部分基于 How do I set a field value in an C# Expression tree? 中的代码和 Assignment in .NET 3.5 expression trees .

public static class FluentTools
{
public static Action<T, TValue> GetterToSetter<T, TValue>(Expression<Func<T, TValue>> getter)
{
ParameterExpression parameter;
Expression instance;
MemberExpression propertyOrField;

GetMemberExpression(getter, out parameter, out instance, out propertyOrField);

// Very simple case: p => p.Property or p => p.Field
if (parameter == instance)
{
if (propertyOrField.Member.MemberType == MemberTypes.Property)
{
// This is FASTER than Expression trees! (5x on my benchmarks) but works only on properties
PropertyInfo property = propertyOrField.Member as PropertyInfo;
MethodInfo setter = property.GetSetMethod();
var action = (Action<T, TValue>)Delegate.CreateDelegate(typeof(Action<T, TValue>), setter);
return action;
}
#region .NET 3.5
else // if (propertyOrField.Member.MemberType == MemberTypes.Field)
{
// 1.2x slower than 4.0 method, 5x faster than 3.5 method
FieldInfo field = propertyOrField.Member as FieldInfo;
var action = FieldSetter<T, TValue>(field);
return action;
}
#endregion
}

ParameterExpression value = Expression.Parameter(typeof(TValue), "val");

Expression expr = null;

#region .NET 3.5
if (propertyOrField.Member.MemberType == MemberTypes.Property)
{
PropertyInfo property = propertyOrField.Member as PropertyInfo;
MethodInfo setter = property.GetSetMethod();
expr = Expression.Call(instance, setter, value);
}
else // if (propertyOrField.Member.MemberType == MemberTypes.Field)
{
expr = FieldSetter(propertyOrField, value);
}
#endregion

//#region .NET 4.0
//// For field access it's 5x faster than the 3.5 method and 1.2x than "simple" method. For property access nearly same speed (1.1x faster).
//expr = Expression.Assign(propertyOrField, value);
//#endregion

return Expression.Lambda<Action<T, TValue>>(expr, parameter, value).Compile();
}

private static void GetMemberExpression<T, U>(Expression<Func<T, U>> expression, out ParameterExpression parameter, out Expression instance, out MemberExpression propertyOrField)
{
Expression current = expression.Body;

while (current.NodeType == ExpressionType.Convert || current.NodeType == ExpressionType.TypeAs)
{
current = (current as UnaryExpression).Operand;
}

if (current.NodeType != ExpressionType.MemberAccess)
{
throw new ArgumentException();
}

propertyOrField = current as MemberExpression;
current = propertyOrField.Expression;

instance = current;

while (current.NodeType != ExpressionType.Parameter)
{
if (current.NodeType == ExpressionType.Convert || current.NodeType == ExpressionType.TypeAs)
{
current = (current as UnaryExpression).Operand;
}
else if (current.NodeType == ExpressionType.MemberAccess)
{
current = (current as MemberExpression).Expression;
}
else
{
throw new ArgumentException();
}
}

parameter = current as ParameterExpression;
}

#region .NET 3.5

// Based on https://stackoverflow.com/questions/321650/how-do-i-set-a-field-value-in-an-c-expression-tree/321686#321686
private static Action<T, TValue> FieldSetter<T, TValue>(FieldInfo field)
{
DynamicMethod m = new DynamicMethod("setter", typeof(void), new Type[] { typeof(T), typeof(TValue) }, typeof(FluentTools));
ILGenerator cg = m.GetILGenerator();

// arg0.<field> = arg1
cg.Emit(OpCodes.Ldarg_0);
cg.Emit(OpCodes.Ldarg_1);
cg.Emit(OpCodes.Stfld, field);
cg.Emit(OpCodes.Ret);

return (Action<T, TValue>)m.CreateDelegate(typeof(Action<T, TValue>));
}

// Based on https://stackoverflow.com/questions/208969/assignment-in-net-3-5-expression-trees/3972359#3972359
private static Expression FieldSetter(Expression left, Expression right)
{
return
Expression.Call(
null,
typeof(FluentTools)
.GetMethod("AssignTo", BindingFlags.NonPublic | BindingFlags.Static)
.MakeGenericMethod(left.Type),
left,
right);
}

private static void AssignTo<T>(ref T left, T right) // note the 'ref', which is
{ // important when assigning
left = right; // to value types!
}

#endregion
}

关于c# - Expression<Func<TModel,string>> 到 Expression<Action<TModel>> "Getter"到 "Setter",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7723744/

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