Skip to content
No description or website provided.
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Jasily.FunctionInvoker.Test
Jasily.FunctionInvoker
.gitattributes
.gitignore
Jasily.FunctionInvoker.sln
LICENSE
README.md

README.md

Jasily.FunctionInvoker

Provide a way to dynamic invoke compiled method.

Install

From Nuget

Install-Package Jasily.FunctionInvoker

Usage

Namespaces:

using Jasily.FunctionInvoker;
using Jasily.FunctionInvoker.ArgumentsResolvers;

For Methods

For example, the static method public static String Join(String separator, IEnumerable<String> values);.

var method = var method = typeof(string).GetMethods()
    .Where(z =>
        z.Name == "Join" &&
        z.GetParameters().Length == 2 &&
        z.GetParameters()[1].ParameterType == typeof(IEnumerable<String>)
    ).Single();
var invoker = FunctionInvoker.CreateInvoker(method).AsStaticMethodInvoker();
invoker.Invoke(new object[] { "s", new string[] { "3", "4" } }); // output: "3s4"

Avoid boxing

Try to avoid return value boxing ?

For example, if the return value is int, you can just replace

var invoker = FunctionInvoker.CreateInvoker(method).AsStaticMethodInvoker();

to

var invoker = FunctionInvoker.CreateInvoker(method).AsStaticMethodInvoker<int>();

All kind of invoker

invoker should be:

  • IStaticMethodInvoker - For any static methods.
  • IStaticMethodInvoker<T> - For known return type.
  • IObjectMethodInvoker - For any instance methods.
  • IObjectMethodInvoker<TObject> - For known instance type.
  • IObjectMethodInvoker<TObject, TResult> - For known instance type and return type.

For Constructor

ConstructorInfo constructor = /* I don't care where do you get this. */;
var invoker = FunctionInvoker.CreateInvoker(constructor);
invoker.Invoke(); // a new instance.

For default(T)

Still use Activator.CreateInstance(typeof(int)); ?

Try this for avoid boxing:

var invoker = FunctionInvoker.CreateDefaultInvoker(typeof(int)).AsConstructorInvoker<int>();
invoker.Invoke(); // 0

IArgumentsResolver

invoker require a IArgumentsResolver to resolve all arguments.

public interface IArgumentsResolver
{
    object Resolve(ParameterInfo parameter);
    T Resolve<T>(ParameterInfo parameter);
}

You can create your own IArgumentsResolver and avoid boxing.

You can’t perform that action at this time.