Using Linq.expressions to dynamically generate mapping methods
1. Let's write a simple class test that contains an ID and a name.
public class Test
{ public int? ID { get; set; } public string Name { get; set; }
}
2. need to manually write simple mapping code, temporarily regardless of the case, such as the code below.
/// <summary>
/// Set properties
/// </summary>
Static void Set(Test t,string name,object value)
{
Switch (name)
{
Case "ID":t.ID = Convert.ToInt32(value);break;
Case "Name": t.Name = Convert.ToString(value); break;
}
}
/// <summary>
/// Get the property
/// </summary>
Static Object Get(Test t, string name)
{
Switch (name)
{
Case "ID": return t.ID;
Case "Name": return t.Name;
}
Return null;
}
3. The goal is to generate the above 2 methods through Expressions, first define the set method of 3 parameters Test, name,value (do not know Expressions click)
ParameterExpression val = Expression.Parameter(typeof(object));//value
ParameterExpression instance = Expression.Parameter(typeof(object));//Test
ParameterExpression nameexp = Expression.Parameter(typeof(string));//name
4. by reflection to get all the attributes set, and then generate case statements according to the property set, the following is the expression of the Set method
Var ps = t.GetProperties();//t is typeof(Test)
List<SwitchCase> lt = new List<SwitchCase>();
Foreach (var n in ps)
{
If (!n.CanWrite) { continue; }
Expression tp = Expression.Convert(val, n.PropertyType); //Type conversion, this is an implicit conversion. Currently does not consider the type of forced conversion
lt.Add(Expression.SwitchCase(Expression.Call(Expression.Convert(instance, t), n.GetSetMethod(), tp), Expression.Constant(n.Name)));
}
Expression p1 = Expression.Switch(nameexp, lt.ToArray());
LambdaExpression exp = Expression.Lambda(p1, instance, nameexp, val);//It can be compiled after converting to LambdaExpression
Action<object, string, object> act = exp.Compile() as Action<object, string, object>;
Expressions for 5.Get methods
var ps = t.GetProperties();
List<SwitchCase> lt = new List<SwitchCase>();
ParameterExpression instance = Expression.Parameter(typeof(object));
ParameterExpression nameexp = Expression.Parameter(typeof(string)); foreach (var n in ps)
{ if (!n.CanRead) { continue; }
lt.Add(Expression.SwitchCase(Expression.Convert(Expression.Call(Expression.Convert(instance, t), n.GetGetMethod(), null), typeof(object)), Expression.Constant(n.Name)));
}
Expression p1 = Expression.Switch(nameexp, Expression.Constant(null), lt.ToArray());
LambdaExpression exp = Expression.Lambda(p1, instance, nameexp);
Func<object, string, object> func = exp.Compile() as Func<object, string, object>;
the above is just a simple case code that illustrates expression to produce a mapping code, View the full code, although more people use emit, but the essence is the same.
Object mappings in the C#orm