Object mappings in the C#orm

Source: Internet
Author: User

Tags: Lin code set NULL ROP Soft class Description Stat

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{   publicintgetset;}     Public string Get Set ; } }

2. need to manually write simple mapping code, temporarily regardless of the case, such as the code below.

        /// <summary>        ///Setting Properties/// </summary>        Static voidSet (Test T,stringNameObjectvalue) {            Switch(name) { Case "ID": T.id = Convert.ToInt32 (value); Break;  Case "Name": T.name = convert.tostring (value); Break; }        }        /// <summary>        ///Get Properties/// </summary>        StaticObject Get (Test t,stringname) {            Switch(name) { Case "ID":returnt.id;  Case "Name":returnT.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= Expression.parameter (  typeof(object= 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

varPS = T.getproperties ();//T is typeof (Test)List<switchcase> LT =NewList<switchcase>();foreach(varNinchPS) {     if(!n.canwrite) {Continue; } Expression TP= Expression.convert (val, N.propertytype);//type conversion, which is an implicit conversion. Forced conversion type not currently consideredLt. 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);//Convert to LambdaExpression to compile
Action<object, string, object> act = exp.compile () as Action<object, String, object>;

Expressions for 5.Get methods

varPS =t.getproperties (); List<SwitchCase> LT =NewList<switchcase>(); ParameterExpression instance= Expression.parameter (typeof(Object)); ParameterExpression Nameexp= Expression.parameter (typeof(string));foreach(varNinchPS) {     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 () asfunc<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.

Tags Index: