Skip to content

Commit

Permalink
add reflection based builders
Browse files Browse the repository at this point in the history
  • Loading branch information
tibel committed Jan 8, 2015
1 parent 83079f1 commit 34f2e17
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 50 deletions.
50 changes: 25 additions & 25 deletions src/Weakly/Builders/ExpressionOpenActionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public Action<object> BuildAction(MethodInfo method)
public Action<object, T> BuildAction<T>(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<Action<object, T>>(body, instance, obj).Compile();
var body = Expression.Call(typedInstance, method, arg0);
return Expression.Lambda<Action<object, T>>(body, instance, arg0).Compile();
}

/// <summary>
Expand All @@ -55,12 +55,12 @@ public Action<object, T> BuildAction<T>(MethodInfo method)
public Action<object, T1, T2> BuildAction<T1, T2>(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<Action<object, T1, T2>>(body, instance, arg1, arg2).Compile();
var body = Expression.Call(typedInstance, method, arg0, arg1);
return Expression.Lambda<Action<object, T1, T2>>(body, instance, arg0, arg1).Compile();
}

/// <summary>
Expand All @@ -76,13 +76,13 @@ public Action<object, T1, T2> BuildAction<T1, T2>(MethodInfo method)
public Action<object, T1, T2, T3> BuildAction<T1, T2, T3>(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<Action<object, T1, T2, T3>>(body, instance, arg1, arg2, arg3).Compile();
var body = Expression.Call(typedInstance, method, arg0, arg1, arg2);
return Expression.Lambda<Action<object, T1, T2, T3>>(body, instance, arg0, arg1, arg2).Compile();
}

/// <summary>
Expand All @@ -99,14 +99,14 @@ public Action<object, T1, T2, T3> BuildAction<T1, T2, T3>(MethodInfo method)
public Action<object, T1, T2, T3, T4> BuildAction<T1, T2, T3, T4>(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<Action<object, T1, T2, T3, T4>>(body, instance, arg1, arg2, arg3, arg4).Compile();
var body = Expression.Call(typedInstance, method, arg0, arg1, arg2, arg3);
return Expression.Lambda<Action<object, T1, T2, T3, T4>>(body, instance, arg0, arg1, arg2, arg3).Compile();
}

/// <summary>
Expand All @@ -124,15 +124,15 @@ public Action<object, T1, T2, T3, T4> BuildAction<T1, T2, T3, T4>(MethodInfo met
public Action<object, T1, T2, T3, T4, T5> BuildAction<T1, T2, T3, T4, T5>(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<Action<object, T1, T2, T3, T4, T5>>(body, instance, arg1, arg2, arg3, arg4, arg5).Compile();
var body = Expression.Call(typedInstance, method, arg0, arg1, arg2, arg3, arg4);
return Expression.Lambda<Action<object, T1, T2, T3, T4, T5>>(body, instance, arg0, arg1, arg2, arg3, arg4).Compile();
}
}
}
50 changes: 25 additions & 25 deletions src/Weakly/Builders/ExpressionOpenFuncBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public Func<object, TResult> BuildFunc<TResult>(MethodInfo method)
public Func<object, T, TResult> BuildFunc<T, TResult>(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<Func<object, T, TResult>>(body, instance, obj).Compile();
var body = Expression.Call(typedInstance, method, arg0);
return Expression.Lambda<Func<object, T, TResult>>(body, instance, arg0).Compile();
}

/// <summary>
Expand All @@ -58,12 +58,12 @@ public Func<object, T, TResult> BuildFunc<T, TResult>(MethodInfo method)
public Func<object, T1, T2, TResult> BuildFunc<T1, T2, TResult>(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<Func<object, T1, T2, TResult>>(body, instance, arg1, arg2).Compile();
var body = Expression.Call(typedInstance, method, arg0, arg1);
return Expression.Lambda<Func<object, T1, T2, TResult>>(body, instance, arg0, arg1).Compile();
}

/// <summary>
Expand All @@ -80,13 +80,13 @@ public Func<object, T1, T2, TResult> BuildFunc<T1, T2, TResult>(MethodInfo metho
public Func<object, T1, T2, T3, TResult> BuildFunc<T1, T2, T3, TResult>(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<Func<object, T1, T2, T3, TResult>>(body, instance, arg1, arg2, arg3).Compile();
var body = Expression.Call(typedInstance, method, arg0, arg1, arg2);
return Expression.Lambda<Func<object, T1, T2, T3, TResult>>(body, instance, arg0, arg1, arg2).Compile();
}

/// <summary>
Expand All @@ -104,14 +104,14 @@ public Func<object, T1, T2, T3, TResult> BuildFunc<T1, T2, T3, TResult>(MethodIn
public Func<object, T1, T2, T3, T4, TResult> BuildFunc<T1, T2, T3, T4, TResult>(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<Func<object, T1, T2, T3, T4, TResult>>(body, instance, arg1, arg2, arg3, arg4).Compile();
var body = Expression.Call(typedInstance, method, arg0, arg1, arg2, arg3);
return Expression.Lambda<Func<object, T1, T2, T3, T4, TResult>>(body, instance, arg0, arg1, arg2, arg3).Compile();
}

/// <summary>
Expand All @@ -130,15 +130,15 @@ public Func<object, T1, T2, T3, T4, TResult> BuildFunc<T1, T2, T3, T4, TResult>(
public Func<object, T1, T2, T3, T4, T5, TResult> BuildFunc<T1, T2, T3, T4, T5, TResult>(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<Func<object, T1, T2, T3, T4, T5, TResult>>(body, instance, arg1, arg2, arg3, arg4, arg5).Compile();
var body = Expression.Call(typedInstance, method, arg0, arg1, arg2, arg3, arg4);
return Expression.Lambda<Func<object, T1, T2, T3, T4, T5, TResult>>(body, instance, arg0, arg1, arg2, arg3, arg4).Compile();
}
}
}
23 changes: 23 additions & 0 deletions src/Weakly/Builders/ReflectionDynamicDelegateBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Reflection;

namespace Weakly.Builders
{
/// <summary>
/// Reflection based <see cref="IDynamicDelegateBuilder"/>.
/// </summary>
public sealed class ReflectionDynamicDelegateBuilder : IDynamicDelegateBuilder
{
/// <summary>
/// Create a dynamic delegate from the specified method.
/// </summary>
/// <param name="method">The method.</param>
/// <returns>
/// The dynamic delegate.
/// </returns>
public Func<object, object[], object> BuildDynamic(MethodInfo method)
{
return method.Invoke;
}
}
}
98 changes: 98 additions & 0 deletions src/Weakly/Builders/ReflectionOpenActionBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Reflection;

namespace Weakly.Builders
{
/// <summary>
/// Reflection based <see cref="IOpenActionBuilder"/>.
/// </summary>
public sealed class ReflectionOpenActionBuilder : IOpenActionBuilder
{
/// <summary>
/// Create an open delegate from the specified method.
/// </summary>
/// <param name="method">The method.</param>
/// <returns>
/// The open delegate.
/// </returns>
public Action<object> BuildAction(MethodInfo method)
{
return instance => method.Invoke(instance, null);
}

/// <summary>
/// Create an open delegate from the specified method.
/// </summary>
/// <typeparam name="T">The type of the parameter of the method that this delegate encapsulates.</typeparam>
/// <param name="method">The method.</param>
/// <returns>
/// The open delegate.
/// </returns>
public Action<object, T> BuildAction<T>(MethodInfo method)
{
return (instance, arg0) => method.Invoke(instance, new object[] {arg0});
}

/// <summary>
/// Create an open delegate from the specified method.
/// </summary>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <param name="method">The method.</param>
/// <returns>
/// The open delegate.
/// </returns>
public Action<object, T1, T2> BuildAction<T1, T2>(MethodInfo method)
{
return (instance, arg0, arg1) => method.Invoke(instance, new object[] { arg0, arg1 });
}

/// <summary>
/// Create an open delegate from the specified method.
/// </summary>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the method that this delegate encapsulates.</typeparam>
/// <param name="method">The method.</param>
/// <returns>
/// The open delegate.
/// </returns>
public Action<object, T1, T2, T3> BuildAction<T1, T2, T3>(MethodInfo method)
{
return (instance, arg0, arg1, arg2) => method.Invoke(instance, new object[] { arg0, arg1, arg2 });
}

/// <summary>
/// Create an open delegate from the specified method.
/// </summary>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter of the method that this delegate encapsulates.</typeparam>
/// <param name="method">The method.</param>
/// <returns>
/// The open delegate.
/// </returns>
public Action<object, T1, T2, T3, T4> BuildAction<T1, T2, T3, T4>(MethodInfo method)
{
return (instance, arg0, arg1, arg2, arg3) => method.Invoke(instance, new object[] { arg0, arg1, arg2, arg3 });
}

/// <summary>
/// Create an open delegate from the specified method.
/// </summary>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T5">The type of the fifth parameter of the method that this delegate encapsulates.</typeparam>
/// <param name="method">The method.</param>
/// <returns>
/// The open delegate.
/// </returns>
public Action<object, T1, T2, T3, T4, T5> BuildAction<T1, T2, T3, T4, T5>(MethodInfo method)
{
return (instance, arg0, arg1, arg2, arg3, arg4) => method.Invoke(instance, new object[] { arg0, arg1, arg2, arg3, arg4 });
}
}
}
Loading

0 comments on commit 34f2e17

Please sign in to comment.