Summarize instance code called with an expression number

Source: Internet
Author: User
Using expression tree to construct delegates to improve reflection performance make a little change just for your own use

    public static class Dynamicmethodbuilder {public static Delegate builddynamicdelegate (MethodInfo MethodInfo, Constr            Uctorinfo ConstructorInfo = null) {if (MethodInfo = = null) throw new ArgumentNullException ("MethodInfo"); list<parameterexpression> paramexpressions = Methodinfo.getparameters ().            Select (p, i) =>{var name = "param" + (i + 1); return Expression.parameter (P.parametertype, name); }).            ToList (); Methodcallexpression callexpression;if (methodinfo.isstatic) {//call (params ...)            Callexpression = Expression.call (MethodInfo, paramexpressions); }else{if (ConstructorInfo! = null) {//instance (params). Call (params ....) list<parameterexpression> constructorparamexpressions = Constructorinfo.getparameters ().                    Select (p, i) =>{var name = "Constparam" + (i + 1); return Expression.parameter (P.parametertype, name); }).                    ToList (); Callexpression = Expression.call (expreSsion.                    New (ConstructorInfo, constructorparamexpressions), MethodInfo, paramexpressions);                Paramexpressions.insertrange (0, constructorparamexpressions); }else{callexpression = Expression.call (Expression.new (Methodinfo.reflectedtype), MethodInfo, paramexpr                Essions); }}return Expression.lambda (Callexpression, paramexpressions).        Compile (); }     }

Test:

    public class Baby {private readonly DateTime _birthday;        Public Baby (DateTime birthDay) {_birthday = BirthDay;        } public Baby () {_birthday = DateTime.Now;  public string Getbabyinfo (string name, int sex) = "Name: {name}, days of birth: {datetime.now-_birthday}, Gender: {(Sex = = 1?    "Male": "Female")} "; } class Program {static void Main (string[] args) {Type TargetType = assembly.getexecuting Assembly ().            GetType ("Consoleapplication1.baby");            MethodInfo MethodInfo = Targettype.getmethod ("Getbabyinfo", new[] {typeof (String), typeof (int)});            ConstructorInfo constructor = Targettype.getconstructor (new[] {typeof (DateTime)});            Withconstructor (MethodInfo, constructor);            Withoutconstructor (MethodInfo);        Console.readkey (); } static void Withconstructor (MethodInfo MethodInfo, ConstructorInfo constructor) {var Func = (func<datetime, string, int, string>) dynamicmethodbuilder.builddynamicdelegate (MethodInfo, constructor)            ;        Console.WriteLine (Func (DateTime.Now.AddDays (-100), "Sugar Pier", 1)); } static void Withoutconstructor (MethodInfo MethodInfo) {var func = (func<string, int, string            >) dynamicmethodbuilder.builddynamicdelegate (methodInfo);        Console.WriteLine (func ("Sugar Pier", 1)); }    }

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.