照着 利用表达式树构建委托改善反射性能 做了一点小更改正好适合自己用

    public static class DynamicMethodBuilder    {public static Delegate BuildDynamicDelegate(MethodInfo methodInfo, ConstructorInfo 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, paramExpressions);                }            }return Expression.Lambda(callExpression, paramExpressions).Compile();        }     }

测试:

    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} , 出生天数:{ DateTime.Now- _birthDay} ,性别 :{(sex == 1 ? "男" : "女")}";    }    class Program    {        static void Main(string[] args)        {            Type targetType = Assembly.GetExecutingAssembly().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), "糖墩儿", 1));        }        static void WithOutConstructor(MethodInfo methodInfo)        {            var func = (Func<string, int, string>)DynamicMethodBuilder.BuildDynamicDelegate(methodInfo);            Console.WriteLine(func("糖墩儿", 1));        }    }

更多相关文章

  1. 详解.net正则表达式怎么使用?
  2. 浅谈WPF之Binding表达式
  3. C#如何使用正则表达式来验证中文字符的案例
  4. C#中的正则表达式如何验证中文字符的实例
  5. C#中关于匿名委托和Lambda表达式的使用详解
  6. C#中关于表达式树的简单介绍
  7. 正则表达式模式匹配字符串基础知识_正则表达式
  8. 第四章C++:表达式概念-运算符的应用
  9. 区分C++常量表达式、const、constexpr(附代码)

随机推荐

  1. android 写 xml时,加layout与不加的区别(如
  2. android:RecyclerView局部刷新那点事~
  3. Android开发工具——ADB(Android(安卓)De
  4. Android热更新实现原理
  5. Android入门学习:Android 系统框架及应用
  6. Android开发100G文档知识大全资料集合
  7. react-native webView android使用本地ht
  8. Android Drawable Resources系列10:
  9. Android跑马灯效果,字体滚动滚屏效果
  10. Android发展史(Android各版本特性-知识篇)