You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Invokation/InvokeWrapper.cs

368 lines
14 KiB

6 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
namespace ZeroLevel.Services.Invokation
{
/// <summary>
/// Method call wrapper
6 years ago
/// </summary>
public class InvokeWrapper : IInvokeWrapper
{
/// <summary>
/// Cahce
6 years ago
/// </summary>
protected readonly Dictionary<string, Invoker> _invokeCachee = new Dictionary<string, Invoker>();
#region Static helpers
/// <summary>
/// Creates a compiled expression for a quick method call, returns the identifier of the expression and a delegate for the call.
6 years ago
/// </summary>
/// <param name="method">Wrapped method</param>
/// <returns>Expression ID and Delegate Tuple</returns>
6 years ago
protected static Tuple<string, Invoker> CreateCompiledExpression(MethodInfo method)
{
var targetArg = Expression.Parameter(typeof(object));
var argsArg = Expression.Parameter(typeof(object[]));
6 years ago
var parameters = method.GetParameters();
Expression body = Expression.Call(
method.IsStatic
? null
: Expression.Convert(targetArg, method.DeclaringType), // the type in which the method is declared
6 years ago
method,
parameters.Select((p, i) =>
Expression.Convert(Expression.ArrayIndex(argsArg, Expression.Constant(i)), p.ParameterType)));
if (body.Type == typeof(void))
body = Expression.Block(body, Expression.Constant(null));
else if (body.Type.IsValueType)
body = Expression.Convert(body, typeof(object));
var identity = CreateMethodIdentity(method.Name, parameters.Select(p => p.ParameterType).ToArray());
return new Tuple<string, Invoker>(identity.ToString(),
Expression.Lambda<Invoker>(body, targetArg, argsArg).Compile());
}
/// <summary>
/// Wraps Delegate Call
6 years ago
/// </summary>
/// <param name="handler">Wrapped delegate</param>
/// <returns>Expression ID and Delegate Tuple</returns>
6 years ago
protected static Tuple<string, Invoker> CreateCompiledExpression(Delegate handler)
{
return CreateCompiledExpression(handler.GetMethodInfo());
}
#endregion Static helpers
6 years ago
#region Helpers
/// <summary>
/// ID uniquely identifying method at the type level (but not at the global level)
6 years ago
/// </summary>
/// <param name="name">Method name</param>
/// <param name="argsTypes">Method Argument Types</param>
6 years ago
/// <returns></returns>
internal static string CreateMethodIdentity(string name, params Type[] argsTypes)
{
var identity = new StringBuilder(name);
identity.Append("(");
if (null != argsTypes)
{
for (var i = 0; i < argsTypes.Length; i++)
{
identity.Append(argsTypes[i].Name);
if ((i + 1) < argsTypes.Length)
identity.Append(".");
}
}
identity.Append(")");
return identity.ToString();
}
#endregion Helpers
6 years ago
#region Configure by Type
public IEnumerable<string> Configure<T>()
{
return Configure(typeof(T));
}
public IEnumerable<string> Configure<T>(string methodName)
{
return Configure(typeof(T), methodName);
}
public IEnumerable<string> Configure<T>(Func<MethodInfo, bool> filter)
{
return Configure(typeof(T), filter);
}
6 years ago
public IEnumerable<string> Configure(Type instanceType)
6 years ago
{
6 years ago
var result = instanceType.GetMethods(BindingFlags.Static |
6 years ago
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.FlattenHierarchy)?.Select(CreateCompiledExpression);
7 months ago
if (result == null) return Enumerable.Empty<string>();
6 years ago
Configure(result);
return result.Select(r => r.Item1).ToList();
}
6 years ago
public IEnumerable<string> Configure(Type instanceType, string methodName)
6 years ago
{
6 years ago
var result = instanceType.GetMethods(BindingFlags.Static |
6 years ago
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.FlattenHierarchy)?.Where(m => m.Name.Equals(methodName))
?.Select(CreateCompiledExpression);
7 months ago
if (result == null) return Enumerable.Empty<string>();
6 years ago
Configure(result);
return result.Select(r => r.Item1).ToList();
}
6 years ago
public IEnumerable<string> ConfigureGeneric<T>(Type instanceType, string methodName)
6 years ago
{
6 years ago
var result = instanceType.GetMethods(BindingFlags.Static |
6 years ago
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.FlattenHierarchy)?.Where(m => m.Name.Equals(methodName))
?.Select(method => method.MakeGenericMethod(typeof(T))).Select(CreateCompiledExpression);
7 months ago
if (result == null) return Enumerable.Empty<string>();
6 years ago
Configure(result);
return result.Select(r => r.Item1).ToList();
}
public IEnumerable<string> ConfigureGeneric(Type instanceType, Type genericType, string methodName)
{
var result = instanceType.GetMethods(BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.FlattenHierarchy)?.Where(m => m.Name.Equals(methodName))
?.Select(method => method.MakeGenericMethod(genericType)).Select(CreateCompiledExpression);
7 months ago
if (result == null) return Enumerable.Empty<string>();
6 years ago
Configure(result);
return result.Select(r => r.Item1).ToList();
}
6 years ago
public IEnumerable<string> ConfigureGeneric<T>(Type instanceType, Func<MethodInfo, bool> filter)
6 years ago
{
6 years ago
var result = instanceType.GetMethods(BindingFlags.Static |
6 years ago
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.FlattenHierarchy)?.Where(filter)
?.Select(method => method.MakeGenericMethod(typeof(T))).Select(CreateCompiledExpression);
7 months ago
if (result == null) return Enumerable.Empty<string>();
Configure(result);
return result.Select(r => r.Item1).ToList();
6 years ago
}
public IEnumerable<string> ConfigureGeneric(Type instanceType, Type genericType, Func<MethodInfo, bool> filter)
{
var result = instanceType.GetMethods(BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.FlattenHierarchy)?.Where(filter)
?.Select(method => method.MakeGenericMethod(genericType)).Select(CreateCompiledExpression);
7 months ago
if (result == null) return Enumerable.Empty<string>();
Configure(result);
return result.Select(r => r.Item1).ToList();
6 years ago
}
6 years ago
public IEnumerable<string> Configure(Type instanceType, Func<MethodInfo, bool> filter)
6 years ago
{
6 years ago
var result = instanceType.GetMethods(BindingFlags.Static |
6 years ago
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.FlattenHierarchy)?.Where(filter)
?.Select(CreateCompiledExpression);
7 months ago
if (result == null) return Enumerable.Empty<string>();
Configure(result);
return result.Select(r => r.Item1).ToList();
6 years ago
}
#endregion Configure by Type
6 years ago
#region Configure by MethodInfo
/// <summary>
/// Cache the specified method
6 years ago
/// </summary>
/// <param name="method">Method</param>
/// <returns>Call ID</returns>
6 years ago
public string Configure(MethodInfo method)
{
var invoke = CreateCompiledExpression(method);
Configure(invoke);
return invoke.Item1;
}
/// <summary>
/// Cache the specified delegate
6 years ago
/// </summary>
/// <param name="handler">Delegate</param>
/// <returns>Call ID</returns>
6 years ago
public string Configure(Delegate handler)
{
var invoke = CreateCompiledExpression(handler);
Configure(invoke);
return invoke.Item1;
}
public IEnumerable<string> Configure(IEnumerable<MethodInfo> list)
{
var result = list?.Select(CreateCompiledExpression);
if (null != result)
{
Configure(result);
return result.Select(r => r.Item1).ToList();
}
return Enumerable.Empty<string>();
}
#endregion Configure by MethodInfo
6 years ago
#region Configuration
/// <summary>
/// Filling the cache from the list of methods with identifiers
6 years ago
/// </summary>
protected void Configure(IEnumerable<Tuple<string, Invoker>> list)
{
foreach (var invoke in list)
{
Configure(invoke);
}
}
/// <summary>
/// Adding a call to the cache
6 years ago
/// </summary>
protected void Configure(Tuple<string, Invoker> invoke)
{
_invokeCachee[invoke.Item1] = invoke.Item2;
}
#endregion Configuration
6 years ago
#region Invoking
/// <summary>
/// Calling a static method by identifier, if there is no method in the cache, a KeyNotFoundException exception will be thrown
6 years ago
/// </summary>
/// <param name="identity">Call ID</param>
/// <param name="args">Method Arguments</param>
/// <returns>Execution result</returns>
6 years ago
public object InvokeStatic(string identity, object[] args)
{
if (_invokeCachee.ContainsKey(identity))
{
7 months ago
return _invokeCachee[identity](null!, args);
6 years ago
}
throw new KeyNotFoundException(String.Format("Not found method with identity '{0}'", identity));
}
/// <summary>
/// Calling a method by identifier; if there is no method in the cache, KeyNotFoundException will be thrown.
6 years ago
/// </summary>
/// <param name="target">The instance on which the method is called</param>
/// <param name="identity">Call ID</param>
/// <param name="args">Method Arguments</param>
/// <returns>Execution result</returns>
6 years ago
public object Invoke(object target, string identity, object[] args)
{
if (_invokeCachee.ContainsKey(identity))
{
return _invokeCachee[identity](target, args);
}
throw new KeyNotFoundException($"Not found method with identity '{identity}'");
6 years ago
}
public object Invoke(object target, string identity)
{
if (_invokeCachee.ContainsKey(identity))
{
7 months ago
return _invokeCachee[identity](target, null!);
6 years ago
}
throw new KeyNotFoundException($"Not found method with identity '{identity}'");
6 years ago
}
/// <summary>
/// Execution of a static cached method
6 years ago
/// </summary>
/// <param name="methodName">Method name</param>
/// <param name="args">Method Arguments</param>
/// /// <returns>Execution result</returns>
6 years ago
public object Invoke(string methodName, object[] args)
{
return InvokeStatic(CreateMethodIdentity(methodName, args.Select(a => a.GetType()).ToArray()), args);
}
#endregion Invoking
6 years ago
#region Helpers
/// <summary>
/// Request call id for method
6 years ago
/// </summary>
/// <param name="methodName">Method name</param>
/// <param name="argsTypes">Method argument type list</param>
/// <returns>Call ID</returns>
6 years ago
public string GetInvokerIdentity(string methodName, params Type[] argsTypes)
{
return CreateMethodIdentity(methodName, argsTypes);
}
/// <summary>
/// Request for delegate to wrap method
6 years ago
/// </summary>
/// <param name="identity">Call ID</param>
/// <returns>Delegate</returns>
6 years ago
public Invoker GetInvoker(string identity)
{
if (_invokeCachee.ContainsKey(identity))
{
return _invokeCachee[identity];
}
7 months ago
return null!;
6 years ago
}
/// <summary>
/// Request for delegate to wrap method
6 years ago
/// </summary>
/// <param name="methodName">Method name</param>
/// <param name="argsTypes">Method argument type list</param>
/// <returns>Delegate</returns>
6 years ago
public Invoker GetInvoker(string methodName, params Type[] argsTypes)
{
return GetInvoker(CreateMethodIdentity(methodName, argsTypes));
}
#endregion Helpers
6 years ago
#region Factories
public static IInvokeWrapper Create()
{
return new InvokeWrapper();
}
#endregion Factories
6 years ago
}
}

Powered by TurnKey Linux.