From 34f2e171e88394a033bf108a4f2c44a0eee4b00f Mon Sep 17 00:00:00 2001 From: Thomas Ibel Date: Thu, 8 Jan 2015 19:31:05 +0100 Subject: [PATCH] add reflection based builders --- .../Builders/ExpressionOpenActionBuilder.cs | 50 ++++----- .../Builders/ExpressionOpenFuncBuilder.cs | 50 ++++----- .../ReflectionDynamicDelegateBuilder.cs | 23 ++++ .../Builders/ReflectionOpenActionBuilder.cs | 98 +++++++++++++++++ .../Builders/ReflectionOpenFuncBuilder.cs | 104 ++++++++++++++++++ .../ReflectionPropertyAccessorBuilder.cs | 34 ++++++ src/Weakly/Weakly.csproj | 4 + 7 files changed, 313 insertions(+), 50 deletions(-) create mode 100644 src/Weakly/Builders/ReflectionDynamicDelegateBuilder.cs create mode 100644 src/Weakly/Builders/ReflectionOpenActionBuilder.cs create mode 100644 src/Weakly/Builders/ReflectionOpenFuncBuilder.cs create mode 100644 src/Weakly/Builders/ReflectionPropertyAccessorBuilder.cs diff --git a/src/Weakly/Builders/ExpressionOpenActionBuilder.cs b/src/Weakly/Builders/ExpressionOpenActionBuilder.cs index 9ce981d..82fa434 100644 --- a/src/Weakly/Builders/ExpressionOpenActionBuilder.cs +++ b/src/Weakly/Builders/ExpressionOpenActionBuilder.cs @@ -36,11 +36,11 @@ public Action BuildAction(MethodInfo method) public Action BuildAction(MethodInfo method) { var instance = Expression.Parameter(typeof(object), "instance"); - var obj = Expression.Parameter(typeof(T), "obj"); + var arg0 = Expression.Parameter(typeof(T), "arg0"); var typedInstance = Expression.Convert(instance, method.DeclaringType); - var body = Expression.Call(typedInstance, method, obj); - return Expression.Lambda>(body, instance, obj).Compile(); + var body = Expression.Call(typedInstance, method, arg0); + return Expression.Lambda>(body, instance, arg0).Compile(); } /// @@ -55,12 +55,12 @@ public Action BuildAction(MethodInfo method) public Action BuildAction(MethodInfo method) { var instance = Expression.Parameter(typeof(object), "instance"); - var arg1 = Expression.Parameter(typeof(T1), "arg1"); - var arg2 = Expression.Parameter(typeof(T2), "arg2"); + var arg0 = Expression.Parameter(typeof(T1), "arg0"); + var arg1 = Expression.Parameter(typeof(T2), "arg1"); var typedInstance = Expression.Convert(instance, method.DeclaringType); - var body = Expression.Call(typedInstance, method, arg1, arg2); - return Expression.Lambda>(body, instance, arg1, arg2).Compile(); + var body = Expression.Call(typedInstance, method, arg0, arg1); + return Expression.Lambda>(body, instance, arg0, arg1).Compile(); } /// @@ -76,13 +76,13 @@ public Action BuildAction(MethodInfo method) public Action BuildAction(MethodInfo method) { var instance = Expression.Parameter(typeof(object), "instance"); - var arg1 = Expression.Parameter(typeof(T1), "arg1"); - var arg2 = Expression.Parameter(typeof(T2), "arg2"); - var arg3 = Expression.Parameter(typeof(T3), "arg3"); + var arg0 = Expression.Parameter(typeof(T1), "arg0"); + var arg1 = Expression.Parameter(typeof(T2), "arg1"); + var arg2 = Expression.Parameter(typeof(T3), "arg2"); var typedInstance = Expression.Convert(instance, method.DeclaringType); - var body = Expression.Call(typedInstance, method, arg1, arg2, arg3); - return Expression.Lambda>(body, instance, arg1, arg2, arg3).Compile(); + var body = Expression.Call(typedInstance, method, arg0, arg1, arg2); + return Expression.Lambda>(body, instance, arg0, arg1, arg2).Compile(); } /// @@ -99,14 +99,14 @@ public Action BuildAction(MethodInfo method) public Action BuildAction(MethodInfo method) { var instance = Expression.Parameter(typeof(object), "instance"); - var arg1 = Expression.Parameter(typeof(T1), "arg1"); - var arg2 = Expression.Parameter(typeof(T2), "arg2"); - var arg3 = Expression.Parameter(typeof(T3), "arg3"); - var arg4 = Expression.Parameter(typeof(T4), "arg4"); + var arg0 = Expression.Parameter(typeof(T1), "arg0"); + var arg1 = Expression.Parameter(typeof(T2), "arg1"); + var arg2 = Expression.Parameter(typeof(T3), "arg2"); + var arg3 = Expression.Parameter(typeof(T4), "arg3"); var typedInstance = Expression.Convert(instance, method.DeclaringType); - var body = Expression.Call(typedInstance, method, arg1, arg2, arg3, arg4); - return Expression.Lambda>(body, instance, arg1, arg2, arg3, arg4).Compile(); + var body = Expression.Call(typedInstance, method, arg0, arg1, arg2, arg3); + return Expression.Lambda>(body, instance, arg0, arg1, arg2, arg3).Compile(); } /// @@ -124,15 +124,15 @@ public Action BuildAction(MethodInfo met public Action BuildAction(MethodInfo method) { var instance = Expression.Parameter(typeof(object), "instance"); - var arg1 = Expression.Parameter(typeof(T1), "arg1"); - var arg2 = Expression.Parameter(typeof(T2), "arg2"); - var arg3 = Expression.Parameter(typeof(T3), "arg3"); - var arg4 = Expression.Parameter(typeof(T4), "arg4"); - var arg5 = Expression.Parameter(typeof(T5), "arg5"); + var arg0 = Expression.Parameter(typeof(T1), "arg0"); + var arg1 = Expression.Parameter(typeof(T2), "arg1"); + var arg2 = Expression.Parameter(typeof(T3), "arg2"); + var arg3 = Expression.Parameter(typeof(T4), "arg3"); + var arg4 = Expression.Parameter(typeof(T5), "arg4"); var typedInstance = Expression.Convert(instance, method.DeclaringType); - var body = Expression.Call(typedInstance, method, arg1, arg2, arg3, arg4, arg5); - return Expression.Lambda>(body, instance, arg1, arg2, arg3, arg4, arg5).Compile(); + var body = Expression.Call(typedInstance, method, arg0, arg1, arg2, arg3, arg4); + return Expression.Lambda>(body, instance, arg0, arg1, arg2, arg3, arg4).Compile(); } } } diff --git a/src/Weakly/Builders/ExpressionOpenFuncBuilder.cs b/src/Weakly/Builders/ExpressionOpenFuncBuilder.cs index 09fbd61..a3a91c3 100644 --- a/src/Weakly/Builders/ExpressionOpenFuncBuilder.cs +++ b/src/Weakly/Builders/ExpressionOpenFuncBuilder.cs @@ -38,11 +38,11 @@ public Func BuildFunc(MethodInfo method) public Func BuildFunc(MethodInfo method) { var instance = Expression.Parameter(typeof(object), "instance"); - var obj = Expression.Parameter(typeof(T), "obj"); + var arg0 = Expression.Parameter(typeof(T), "arg0"); var typedInstance = Expression.Convert(instance, method.DeclaringType); - var body = Expression.Call(typedInstance, method, obj); - return Expression.Lambda>(body, instance, obj).Compile(); + var body = Expression.Call(typedInstance, method, arg0); + return Expression.Lambda>(body, instance, arg0).Compile(); } /// @@ -58,12 +58,12 @@ public Func BuildFunc(MethodInfo method) public Func BuildFunc(MethodInfo method) { var instance = Expression.Parameter(typeof(object), "instance"); - var arg1 = Expression.Parameter(typeof(T1), "arg1"); - var arg2 = Expression.Parameter(typeof(T2), "arg2"); + var arg0 = Expression.Parameter(typeof(T1), "arg0"); + var arg1 = Expression.Parameter(typeof(T2), "arg1"); var typedInstance = Expression.Convert(instance, method.DeclaringType); - var body = Expression.Call(typedInstance, method, arg1, arg2); - return Expression.Lambda>(body, instance, arg1, arg2).Compile(); + var body = Expression.Call(typedInstance, method, arg0, arg1); + return Expression.Lambda>(body, instance, arg0, arg1).Compile(); } /// @@ -80,13 +80,13 @@ public Func BuildFunc(MethodInfo metho public Func BuildFunc(MethodInfo method) { var instance = Expression.Parameter(typeof(object), "instance"); - var arg1 = Expression.Parameter(typeof(T1), "arg1"); - var arg2 = Expression.Parameter(typeof(T2), "arg2"); - var arg3 = Expression.Parameter(typeof(T3), "arg3"); + var arg0 = Expression.Parameter(typeof(T1), "arg0"); + var arg1 = Expression.Parameter(typeof(T2), "arg1"); + var arg2 = Expression.Parameter(typeof(T3), "arg2"); var typedInstance = Expression.Convert(instance, method.DeclaringType); - var body = Expression.Call(typedInstance, method, arg1, arg2, arg3); - return Expression.Lambda>(body, instance, arg1, arg2, arg3).Compile(); + var body = Expression.Call(typedInstance, method, arg0, arg1, arg2); + return Expression.Lambda>(body, instance, arg0, arg1, arg2).Compile(); } /// @@ -104,14 +104,14 @@ public Func BuildFunc(MethodIn public Func BuildFunc(MethodInfo method) { var instance = Expression.Parameter(typeof(object), "instance"); - var arg1 = Expression.Parameter(typeof(T1), "arg1"); - var arg2 = Expression.Parameter(typeof(T2), "arg2"); - var arg3 = Expression.Parameter(typeof(T3), "arg3"); - var arg4 = Expression.Parameter(typeof(T4), "arg4"); + var arg0 = Expression.Parameter(typeof(T1), "arg0"); + var arg1 = Expression.Parameter(typeof(T2), "arg1"); + var arg2 = Expression.Parameter(typeof(T3), "arg2"); + var arg3 = Expression.Parameter(typeof(T4), "arg3"); var typedInstance = Expression.Convert(instance, method.DeclaringType); - var body = Expression.Call(typedInstance, method, arg1, arg2, arg3, arg4); - return Expression.Lambda>(body, instance, arg1, arg2, arg3, arg4).Compile(); + var body = Expression.Call(typedInstance, method, arg0, arg1, arg2, arg3); + return Expression.Lambda>(body, instance, arg0, arg1, arg2, arg3).Compile(); } /// @@ -130,15 +130,15 @@ public Func BuildFunc( public Func BuildFunc(MethodInfo method) { var instance = Expression.Parameter(typeof(object), "instance"); - var arg1 = Expression.Parameter(typeof(T1), "arg1"); - var arg2 = Expression.Parameter(typeof(T2), "arg2"); - var arg3 = Expression.Parameter(typeof(T3), "arg3"); - var arg4 = Expression.Parameter(typeof(T4), "arg4"); - var arg5 = Expression.Parameter(typeof(T5), "arg5"); + var arg0 = Expression.Parameter(typeof(T1), "arg0"); + var arg1 = Expression.Parameter(typeof(T2), "arg1"); + var arg2 = Expression.Parameter(typeof(T3), "arg2"); + var arg3 = Expression.Parameter(typeof(T4), "arg3"); + var arg4 = Expression.Parameter(typeof(T5), "arg4"); var typedInstance = Expression.Convert(instance, method.DeclaringType); - var body = Expression.Call(typedInstance, method, arg1, arg2, arg3, arg4, arg5); - return Expression.Lambda>(body, instance, arg1, arg2, arg3, arg4, arg5).Compile(); + var body = Expression.Call(typedInstance, method, arg0, arg1, arg2, arg3, arg4); + return Expression.Lambda>(body, instance, arg0, arg1, arg2, arg3, arg4).Compile(); } } } diff --git a/src/Weakly/Builders/ReflectionDynamicDelegateBuilder.cs b/src/Weakly/Builders/ReflectionDynamicDelegateBuilder.cs new file mode 100644 index 0000000..feda334 --- /dev/null +++ b/src/Weakly/Builders/ReflectionDynamicDelegateBuilder.cs @@ -0,0 +1,23 @@ +using System; +using System.Reflection; + +namespace Weakly.Builders +{ + /// + /// Reflection based . + /// + public sealed class ReflectionDynamicDelegateBuilder : IDynamicDelegateBuilder + { + /// + /// Create a dynamic delegate from the specified method. + /// + /// The method. + /// + /// The dynamic delegate. + /// + public Func BuildDynamic(MethodInfo method) + { + return method.Invoke; + } + } +} diff --git a/src/Weakly/Builders/ReflectionOpenActionBuilder.cs b/src/Weakly/Builders/ReflectionOpenActionBuilder.cs new file mode 100644 index 0000000..3cf3299 --- /dev/null +++ b/src/Weakly/Builders/ReflectionOpenActionBuilder.cs @@ -0,0 +1,98 @@ +using System; +using System.Reflection; + +namespace Weakly.Builders +{ + /// + /// Reflection based . + /// + public sealed class ReflectionOpenActionBuilder : IOpenActionBuilder + { + /// + /// Create an open delegate from the specified method. + /// + /// The method. + /// + /// The open delegate. + /// + public Action BuildAction(MethodInfo method) + { + return instance => method.Invoke(instance, null); + } + + /// + /// Create an open delegate from the specified method. + /// + /// The type of the parameter of the method that this delegate encapsulates. + /// The method. + /// + /// The open delegate. + /// + public Action BuildAction(MethodInfo method) + { + return (instance, arg0) => method.Invoke(instance, new object[] {arg0}); + } + + /// + /// Create an open delegate from the specified method. + /// + /// The type of the first parameter of the method that this delegate encapsulates. + /// The type of the second parameter of the method that this delegate encapsulates. + /// The method. + /// + /// The open delegate. + /// + public Action BuildAction(MethodInfo method) + { + return (instance, arg0, arg1) => method.Invoke(instance, new object[] { arg0, arg1 }); + } + + /// + /// Create an open delegate from the specified method. + /// + /// The type of the first parameter of the method that this delegate encapsulates. + /// The type of the second parameter of the method that this delegate encapsulates. + /// The type of the third parameter of the method that this delegate encapsulates. + /// The method. + /// + /// The open delegate. + /// + public Action BuildAction(MethodInfo method) + { + return (instance, arg0, arg1, arg2) => method.Invoke(instance, new object[] { arg0, arg1, arg2 }); + } + + /// + /// Create an open delegate from the specified method. + /// + /// The type of the first parameter of the method that this delegate encapsulates. + /// The type of the second parameter of the method that this delegate encapsulates. + /// The type of the third parameter of the method that this delegate encapsulates. + /// The type of the fourth parameter of the method that this delegate encapsulates. + /// The method. + /// + /// The open delegate. + /// + public Action BuildAction(MethodInfo method) + { + return (instance, arg0, arg1, arg2, arg3) => method.Invoke(instance, new object[] { arg0, arg1, arg2, arg3 }); + } + + /// + /// Create an open delegate from the specified method. + /// + /// The type of the first parameter of the method that this delegate encapsulates. + /// The type of the second parameter of the method that this delegate encapsulates. + /// The type of the third parameter of the method that this delegate encapsulates. + /// The type of the fourth parameter of the method that this delegate encapsulates. + /// The type of the fifth parameter of the method that this delegate encapsulates. + /// The method. + /// + /// The open delegate. + /// + public Action BuildAction(MethodInfo method) + { + return (instance, arg0, arg1, arg2, arg3, arg4) => method.Invoke(instance, new object[] { arg0, arg1, arg2, arg3, arg4 }); + } + } +} diff --git a/src/Weakly/Builders/ReflectionOpenFuncBuilder.cs b/src/Weakly/Builders/ReflectionOpenFuncBuilder.cs new file mode 100644 index 0000000..3d8defb --- /dev/null +++ b/src/Weakly/Builders/ReflectionOpenFuncBuilder.cs @@ -0,0 +1,104 @@ +using System; +using System.Reflection; + +namespace Weakly.Builders +{ + /// + /// Reflection based . + /// + public sealed class ReflectionOpenFuncBuilder : IOpenFuncBuilder + { + /// + /// Create an open delegate from the specified method. + /// + /// The type of the return value of the method that this delegate encapsulates. + /// The method. + /// + /// The open delegate. + /// + public Func BuildFunc(MethodInfo method) + { + return instance => (TResult) method.Invoke(instance, null); + } + + /// + /// Create an open delegate from the specified method. + /// + /// The type of the parameter of the method that this delegate encapsulates. + /// The type of the return value of the method that this delegate encapsulates. + /// The method. + /// + /// The open delegate. + /// + public Func BuildFunc(MethodInfo method) + { + return (instance, arg0) => (TResult) method.Invoke(instance, new object[] {arg0}); + } + + /// + /// Create an open delegate from the specified method. + /// + /// The type of the first parameter of the method that this delegate encapsulates. + /// The type of the second parameter of the method that this delegate encapsulates. + /// The type of the return value of the method that this delegate encapsulates. + /// The method. + /// + /// The open delegate. + /// + public Func BuildFunc(MethodInfo method) + { + return (instance, arg0, arg1) => (TResult)method.Invoke(instance, new object[] { arg0, arg1 }); + } + + /// + /// Create an open delegate from the specified method. + /// + /// The type of the first parameter of the method that this delegate encapsulates. + /// The type of the second parameter of the method that this delegate encapsulates. + /// The type of the third parameter of the method that this delegate encapsulates. + /// The type of the return value of the method that this delegate encapsulates. + /// The method. + /// + /// The open delegate + /// + public Func BuildFunc(MethodInfo method) + { + return (instance, arg0, arg1, arg2) => (TResult) method.Invoke(instance, new object[] {arg0, arg1, arg2}); + } + + /// + /// Create an open delegate from the specified method. + /// + /// The type of the first parameter of the method that this delegate encapsulates. + /// The type of the second parameter of the method that this delegate encapsulates. + /// The type of the third parameter of the method that this delegate encapsulates. + /// The type of the fourth parameter of the method that this delegate encapsulates. + /// The type of the return value of the method that this delegate encapsulates. + /// The method. + /// + /// The open delegate + /// + public Func BuildFunc(MethodInfo method) + { + return (instance, arg0, arg1, arg2, arg3) => (TResult)method.Invoke(instance, new object[] { arg0, arg1, arg2, arg3 }); + } + + /// + /// Create an open delegate from the specified method. + /// + /// The type of the first parameter of the method that this delegate encapsulates. + /// The type of the second parameter of the method that this delegate encapsulates. + /// The type of the third parameter of the method that this delegate encapsulates. + /// The type of the fourth parameter of the method that this delegate encapsulates. + /// The type of the fifth parameter of the method that this delegate encapsulates. + /// The type of the return value of the method that this delegate encapsulates. + /// The method. + /// + /// The open delegate + /// + public Func BuildFunc(MethodInfo method) + { + return (instance, arg0, arg1, arg2, arg3, arg4) => (TResult)method.Invoke(instance, new object[] { arg0, arg1, arg2, arg3, arg4 }); + } + } +} diff --git a/src/Weakly/Builders/ReflectionPropertyAccessorBuilder.cs b/src/Weakly/Builders/ReflectionPropertyAccessorBuilder.cs new file mode 100644 index 0000000..c45680c --- /dev/null +++ b/src/Weakly/Builders/ReflectionPropertyAccessorBuilder.cs @@ -0,0 +1,34 @@ +using System; + +namespace Weakly.Builders +{ + /// + /// Reflection based . + /// + public sealed class ReflectionPropertyAccessorBuilder : IPropertyAccessorBuilder + { + /// + /// Get compiled Getter function from a given . + /// + /// The property. + /// + /// The function to get the property value. + /// + public Func BuildGetter(System.Reflection.PropertyInfo property) + { + return property.GetValue; + } + + /// + /// Get compiled Setter function from a given . + /// + /// The property. + /// + /// The function to set the property value. + /// + public Action BuildSetter(System.Reflection.PropertyInfo property) + { + return property.SetValue; + } + } +} diff --git a/src/Weakly/Weakly.csproj b/src/Weakly/Weakly.csproj index 2309579..263cddc 100644 --- a/src/Weakly/Weakly.csproj +++ b/src/Weakly/Weakly.csproj @@ -53,7 +53,11 @@ + + + +