diff options
Diffstat (limited to 'IrcStates/Extensions.cs')
-rw-r--r-- | IrcStates/Extensions.cs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/IrcStates/Extensions.cs b/IrcStates/Extensions.cs new file mode 100644 index 0000000..dde0edb --- /dev/null +++ b/IrcStates/Extensions.cs @@ -0,0 +1,32 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; + +namespace IrcStates +{ + public static class Extensions + { + public static Delegate CreateDelegate(this MethodInfo methodInfo, object target) + { + if (methodInfo == null) return null; + + var types = methodInfo.GetParameters().Select(p => p.ParameterType); + + Func<Type[], Type> getType; + if (methodInfo.ReturnType == typeof(void)) + { + getType = Expression.GetActionType; + } + else + { + getType = Expression.GetFuncType; + types = types.Concat(new[] {methodInfo.ReturnType}); + } + + return methodInfo.IsStatic + ? Delegate.CreateDelegate(getType(types.ToArray()), methodInfo) + : Delegate.CreateDelegate(getType(types.ToArray()), target, methodInfo); + } + } +} |