我试图弄清楚如何从代表动作“语句”行的字符串集合中生成一个动作......
using System.Linq.Dynamic;
Action<T> BuildAction<T>(T sourceObject, T destinationObject) where T : BaseThing
{
var source = Expression.Parameter(sourceObject.GetType(), "source");
var destination = Expression.Parameter(destinationObject.GetType(), "destination");
var statements = new[] {
"destination.Foo = source.Foo",
"destination.X = source.Y"
};
var parsedStatements = statements.Select(s => DynamicExpression.Parse(new[] { destination, source }, typeof(void), s);
return Expression.Lambda<Action<T>>(Expression.Block(parsedStatements));
}
这个想法是最终得到类似......
Action<T> result = (destination, source) => {
destination.Foo = source.Foo;
destination.X = source.Y;
};
我的另一个问题是源和目标不必是相同的类型,它们只共享一个基类型,所以在这个例子中,目标可能没有 Y 属性,源可能没有 X 属性(因此映射)。
守着一只汪
相关分类