使用Emit动态调用方法(技术原型2)
2009-12-13 16:43
549 查看
研究了一下,发现系统已经有这类的函数,晕吧!! 实现一个类似Func和Action的方法吧.
调用示例如下:
object inst = new Program();
decimal ret = inst.Dynamic<Guid, int, decimal>("HelloWorld", Guid.NewGuid(), 2009);
原码如下:
调用示例如下:
object inst = new Program();
decimal ret = inst.Dynamic<Guid, int, decimal>("HelloWorld", Guid.NewGuid(), 2009);
原码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection.Emit; using System.Reflection; using Evlon.Util.Dynamic; namespace ConsoleApplication2008 { public class Program { static void Main(string[] args) { object inst = new Program(); decimal ret = inst.Dynamic<Guid, int, decimal>("HelloWorld", Guid.NewGuid(), 2009); Console.WriteLine(ret); Console.ReadLine(); } public decimal HelloWorld(Guid userId, int year) { return userId.GetHashCode() + year; } } } namespace Evlon.Util.Dynamic { public static class TypeExtension { public static TResult Dynamic<T, TResult>(this object inst, string methodName, T arg) { return (TResult)Delegate.CreateDelegate(typeof(Func<T, TResult>), inst, methodName).DynamicInvoke(arg); } public static TResult Dynamic<T1, T2, TResult>(this object inst, string methodName, T1 arg1, T2 arg2) { return (TResult)Delegate.CreateDelegate(typeof(Func<T1, T2, TResult>), inst, methodName).DynamicInvoke(new object[] { arg1, arg2 }); } public static TResult Dynamic<T1, T2, T3, TResult>(this object inst, string methodName, T1 arg1, T2 arg2, T3 arg3) { return (TResult)Delegate.CreateDelegate(typeof(Func<T1, T2, T3, TResult>), inst, methodName).DynamicInvoke(new object[] { arg1, arg2, arg3 }); } public static TResult Dynamic<T1, T2, T3,T4, TResult>(this object inst, string methodName, T1 arg1, T2 arg2, T3 arg3,T4 arg4) { return (TResult)Delegate.CreateDelegate(typeof(Func<T1, T2, T3, T4, TResult>), inst, methodName).DynamicInvoke(new object[] { arg1, arg2, arg3, arg4 }); } public static void Dynamic<T>(this object inst, string methodName, T arg) { Delegate.CreateDelegate(typeof(Action<T>), inst, methodName).DynamicInvoke(arg); } public static void Dynamic<T1, T2>(this object inst, string methodName, T1 arg1, T2 arg2) { Delegate.CreateDelegate(typeof(Action<T1, T2>), inst, methodName).DynamicInvoke(new object[] { arg1, arg2 }); } public static void Dynamic<T1, T2, T3>(this object inst, string methodName, T1 arg1, T2 arg2, T3 arg3) { Delegate.CreateDelegate(typeof(Action<T1, T2, T3>), inst, methodName).DynamicInvoke(new object[] { arg1, arg2, arg3 }); } public static void Dynamic<T1, T2, T3, T4>(this object inst, string methodName, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { Delegate.CreateDelegate(typeof(Action<T1, T2, T3, T4>), inst, methodName).DynamicInvoke(new object[] { arg1, arg2, arg3, arg4 }); } } }
相关文章推荐
- 使用Emit动态调用方法(技术原型)
- 安卓四大组件之二#2-使用AIDL技术实现动态调用其他app中的Service的方法
- 动态方法调用和使用action元素的method属性
- [c#美味] 使用反射动态创建实例并调用方法
- struts2.5 使用感叹号和通配符实现动态方法调用无效的问题及解决!
- 一起谈.NET技术,.NET动态调用DLL的方法
- 使用反射、泛型和委托,动态调用对象的属性和方法——性能和灵活性兼备的方法
- 使用Action 的动态方法调用
- 使用ajax技术无刷新动态调用股票信息
- struts2中动态方法调用和使用通配符定义Action
- 动态方法调用和使用通配符实现类似struts1.x的dispatchAction功能
- Struts2学习笔记(二) 使用通配符动态调用方法
- 使用<a>标签调用struts2的action、传递动态参数、以及参数中出现加号等特殊字符的处理方法
- struts中动态方法调用使用通配符
- 第11章 动态方法调用和使用通配符定义action
- C#反射动态调用dll中的方法及使用QuartZ.net实现作业调度
- 关于Struts2 使用! 感叹号 动态调用Action中的方法
- Struts2使用!动态方法调用无效(转载)
- Struts2使用!动态方法调用无效
- Struts2_动态方法调用和使用通配符定义action