I have this injector which inject data using 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);
}
}
i only pass a collection of Expression and value.
Expression on which where to assign the value (eg. a => a.Code) and value (eg. "123456")
i want to assign it to an object T but with my current attempt, i got a System.ArgumentException: Incorrect number of parameters supplied for lambda declaration. I am stuck and don't know what to do next. :) can anyone point me to the right direction on how to make this work?
This is the sample usage
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);
i also tried
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);
and i can see a progress here because the private property is already assigned but the getter is NULL
Thanks in advance
any help would be appreciated.