zoukankan      html  css  js  c++  java
  • 添加一种emit的应用,反射发出,较直接调用稍慢,但好过反射与表达式树。

    System.Reflection.MethodInfo mInfo = typeof(TypeParse).GetMethod("Add", 
    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    

      





    [Serializable] void Main() { var addMethod = typeof(MyMath).GetMethod("Add"); var dynamicMethod = new DynamicMethod("", typeof(int), new[] { typeof(MyMath), typeof(int), typeof(int) }); // var il = dynamicMethod.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Ldarg_2); il.Emit(OpCodes.Callvirt, addMethod); il.Emit(OpCodes.Ret); // var add = (Func<MyMath, int, int, int>)dynamicMethod.CreateDelegate(typeof(Func<MyMath, int, int, int>)); // var math = new MyMath(); var result = add(math, 1, 2); var method=MethodBase.GetCurrentMethod(); Console.WriteLine (method.Name); var body=method.GetMethodBody(); var attrs=method.CustomAttributes; foreach (var attr in attrs) { Console.WriteLine (attr.AttributeType); } Console.WriteLine (body.GetILAsByteArray()); var pars=method.GetParameters(); foreach (var param in pars) { Console.WriteLine (param.Name); } } // Define other methods and classes here string Get(){ return "dd";} public class MyMath { public int Add(int a, int b) { return a + b; } } } class SerializableAttribute :System.Attribute{ public SerializableAttribute() { }

      

  • 相关阅读:
    C# Socket编程
    C# Socket编程
    Android基础入门教程
    Android基础入门教程
    SQL Server查询事务
    SQL Server查询事务
    SQL事务用法begin tran,commit tran和rollback tran的用法
    SQL事务用法begin tran,commit tran和rollback tran的用法
    SQL Server Insert操作中的锁
    SQL Server Insert操作中的锁
  • 原文地址:https://www.cnblogs.com/hualiu0/p/4943168.html
Copyright © 2011-2022 走看看