Ent*_*pin 5 c# lambda expression
我有这个使用Expression注入数据的注入器
/// <summary>
/// Inject a data to an instance of T
/// </summary>
/// <typeparam name="T">The type of object as parameter</typeparam>
/// <param name="data">Object instance to where the data is injected</param>
/// <param name="pairData">Set of data info</param>
public static void InjectData<T>(this T data, Dictionary<Expression<Func<T, object>>, object> pairData) where T : IAuditable
{
// posible content of T data
// Employee
// : Code
// : Name
// : ID
// possible content of pairData
// Key: a => a.Code
// Value: "12345"
// Key: a => a.Name
// Value: "Vincent"
// after the injection, the data (Employee) will now have value on each properties: Code, Name
foreach (var _o in pairData)
{
var _value = Expression.Constant(_o.Value);
var _assign = Expression.Assign(_o.Key, _value);
//_lambda.Compile()(data);
//var _lambda = Expression.Lambda<Action<object, T>>(_assign, _value, _o.Key);
//_lambda.Compile()(_o.Value, data);
}
}
Run Code Online (Sandbox Code Playgroud)
我只传递表达式和值的集合.
在哪里分配值(例如a => a.Code)和值(例如"123456")的表达式
我想将它分配给一个对象T但是在我当前的尝试中,我得到了一个System.ArgumentException:为lambda声明提供的参数数量不正确.我被困住了,不知道接下来该做什么.:)任何人都可以指出我如何使这项工作的正确方向?
这是示例用法
Employee _employee = new Employee();
Dictionary<Expression<Func<Employee, object>>, object> _x = new Dictionary
<Expression<Func<Employee, object>>, object>
{
{a => a.Code, "12345"}
};
_employee.InjectData(_x);
Assert.AreEqual("12345", _employee.Code);
Run Code Online (Sandbox Code Playgroud)
我也试过了
foreach (var _o in pairData)
{
var _mem = _o.Key.Body as MemberExpression;
var _prop = _mem.Member as PropertyInfo;
var _setMethod = _prop.GetSetMethod();
_prop.SetValue(data, _o.Value);
Run Code Online (Sandbox Code Playgroud)
我可以在这里看到进展,因为私有属性已经分配但是getter是NULL
提前致谢
任何帮助,将不胜感激.
对我来说没有太大意义,但它是:
public static void InjectData<T>(this T data, Dictionary<Expression<Func<T, object>>, object> pairData) where T : IAuditable
{
foreach (var item in pairData)
{
var member = item.Key;
// If member type is a reference type, then member.Body is the property accessor.
// For value types it is Convert(property accessor)
var memberBody = member.Body as MemberExpression ?? (MemberExpression)((UnaryExpression)member.Body).Operand;
var lambda = Expression.Lambda<Action<T>>(Expression.Assign(memberBody, Expression.Constant(item.Value)), member.Parameters);
var action = lambda.Compile();
action(data);
}
}
Run Code Online (Sandbox Code Playgroud)
更新根据评论中的要求,支持嵌套类属性初始化的版本:
public static void InjectData<T>(this T data, Dictionary<Expression<Func<T, object>>, object> pairData) //where T : IAuditable
{
var assignments = new List<Expression>();
foreach (var item in pairData)
{
var member = item.Key;
// If member type is a reference type, then member.Body is the property accessor.
// For value types it is Convert(property accessor)
var memberBody = member.Body as MemberExpression ?? (MemberExpression)((UnaryExpression)member.Body).Operand;
assignments.Clear();
assignments.Add(Expression.Assign(memberBody, Expression.Constant(item.Value)));
var target = member.Parameters[0];
while (memberBody.Expression != target)
{
var childMember = (MemberExpression)memberBody.Expression;
assignments.Add(Expression.IfThen(Expression.ReferenceEqual(childMember, Expression.Constant(null)),
Expression.Assign(childMember, Expression.New(childMember.Type))));
memberBody = childMember;
}
assignments.Reverse();
var body = assignments.Count > 1 ? Expression.Block(assignments) : assignments[0];
var lambda = Expression.Lambda<Action<T>>(body, target);
var action = lambda.Compile();
action(data);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1375 次 |
| 最近记录: |