Object mappings in the C#orm

Source: Internet
Author: User



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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.