16

My application works with loading dll's dynamically, based on settings from the database (file, class and method names). To facilitate, expedite and reduce the use of reflection I would like to have a cache....

Following the idea that using:

 MethodInfo.Invoke

Is nothing performative ( Reflection Performance - Create Delegate (Properties C#)) I would like to translate any call to methods. I thought of something that would work like this:

public static T Create<T>(Type type, string methodName) // or
public static T Create<T>(MethodInfo info) // to use like this:
var action = Create<Action<object>>(typeof(Foo), "AnySetValue");

One requirement is that all the parameters, can be object.

I'm trying to deal with expressions, and so far I have something like this:

    private void Sample()
    {
        var assembly = Assembly.GetAssembly(typeof(Foo));

        Type customType = assembly.GetType("Foo");

        var actionMethodInfo = customType.GetMethod("AnyMethod");
        var funcMethodInfo = customType.GetMethod("AnyGetString");
        var otherActionMethod = customType.GetMethod("AnySetValue");
        var otherFuncMethodInfo = customType.GetMethod("OtherGetString");

        var foo = Activator.CreateInstance(customType);
        var actionAccessor = (Action<object>)BuildSimpleAction(actionMethodInfo);
        actionAccessor(foo);

        var otherAction = (Action<object, object>)BuildOtherAction(otherActionMethod);
        otherAction(foo, string.Empty);

        var otherFuncAccessor = (Func<object, object>)BuildFuncAccessor(funcMethodInfo);
        otherFuncAccessor(foo);

        var funcAccessor = (Func<object,object,object>)BuildOtherFuncAccessor(otherFuncMethodInfo);
        funcAccessor(foo, string.Empty);
    }

    static Action<object> BuildSimpleAction(MethodInfo method)
    {
        var obj = Expression.Parameter(typeof(object), "o");

        Expression<Action<object>> expr =
            Expression.Lambda<Action<object>>(
                Expression.Call(
                    Expression.Convert(obj, method.DeclaringType),
                    method), obj);

        return expr.Compile();
    }

    static Func<object, object> BuildFuncAccessor(MethodInfo method)
    {
        var obj = Expression.Parameter(typeof(object), "o");

        Expression<Func<object, object>> expr =
            Expression.Lambda<Func<object, object>>(
                Expression.Convert(
                    Expression.Call(
                        Expression.Convert(obj, method.DeclaringType),
                        method),
                    typeof(object)),
                obj);

        return expr.Compile();

    }

    static Func<object, object, object> BuildOtherFuncAccessor(MethodInfo method)
    {
        var obj = Expression.Parameter(typeof(object), "o");
        var value = Expression.Parameter(typeof(object));

        Expression<Func<object, object, object>> expr =
            Expression.Lambda<Func<object, object, object>>(
                    Expression.Call(
                        Expression.Convert(obj, method.DeclaringType),
                        method,
                        Expression.Convert(value, method.GetParameters()[0].ParameterType)), 
                        obj, value);

        return expr.Compile();

    }

    static Action<object, object> BuildOtherAction(MethodInfo method)
    {
        var obj = Expression.Parameter(typeof(object), "o");
        var value = Expression.Parameter(typeof(object));

        Expression<Action<object, object>> expr =
            Expression.Lambda<Action<object, object>>(
                Expression.Call(
                    Expression.Convert(obj, method.DeclaringType),
                    method,
                    Expression.Convert(value, method.GetParameters()[0].ParameterType)),
                obj,
                value);

        return expr.Compile();
    }

public class Foo
{
    public void AnyMethod() {}

    public void AnySetValue(string value) {}

    public string AnyGetString()
    {            return string.Empty;        }

    public string OtherGetString(string value)
    {            return string.Empty;        }
}

Is there any way to simplify this code? (I believe it is possible to create a method only using generic..) And when you have 3, 4, 5, any parameters as I do?


I was thinking, what if there was something like this:

https://codereview.stackexchange.com/questions/1070/generic-advanced-delegate-createdelegate-using-expression-trees

but I'll have more one parameter (in action or function), this parameter (first parameter) an object to perform. Is this possible?

Community
  • 1
  • 1
J. Lennon
  • 3,311
  • 4
  • 33
  • 64
  • 1
    No comments on my answer... have you tried it? Was it helpful? Do you still need help? – Miguel Angelo Nov 25 '12 at 23:32
  • @MiguelAngelo sorry, your code actually looks like the best option!!! I believe that is exactly what I am looking for, only wish some time to make some more tests :) – J. Lennon Nov 25 '12 at 23:49

7 Answers7

19

I have made a sample program that fulfills all your requirements (I think!)

class Program
{
    class MyType
    {
        public MyType(int i) { this.Value = i; }

        public void SetValue(int i) { this.Value = i; }

        public void SetSumValue(int a, int b) { this.Value = a + b; }

        public int Value { get; set; }
    }

    public static void Main()
    {
        Type type = typeof(MyType);

        var mi = type.GetMethod("SetValue");

        var obj1 = new MyType(1);
        var obj2 = new MyType(2);

        var action = DelegateBuilder.BuildDelegate<Action<object, int>>(mi);

        action(obj1, 3);
        action(obj2, 4);

        Console.WriteLine(obj1.Value);
        Console.WriteLine(obj2.Value);

        // Sample passing a default value for the 2nd param of SetSumValue.
        var mi2 = type.GetMethod("SetSumValue");

        var action2 = DelegateBuilder.BuildDelegate<Action<object, int>>(mi2, 10);

        action2(obj1, 3);
        action2(obj2, 4);

        Console.WriteLine(obj1.Value);
        Console.WriteLine(obj2.Value);

        // Sample without passing a default value for the 2nd param of SetSumValue.
        // It will just use the default int value that is 0.
        var action3 = DelegateBuilder.BuildDelegate<Action<object, int>>(mi2);

        action3(obj1, 3);
        action3(obj2, 4);

        Console.WriteLine(obj1.Value);
        Console.WriteLine(obj2.Value);
    }
}

DelegateBuilder class:

public class DelegateBuilder
{
    public static T BuildDelegate<T>(MethodInfo method, params object[] missingParamValues)
    {
        var queueMissingParams = new Queue<object>(missingParamValues);

        var dgtMi = typeof(T).GetMethod("Invoke");
        var dgtRet = dgtMi.ReturnType;
        var dgtParams = dgtMi.GetParameters();

        var paramsOfDelegate = dgtParams
            .Select(tp => Expression.Parameter(tp.ParameterType, tp.Name))
            .ToArray();

        var methodParams = method.GetParameters();

        if (method.IsStatic)
        {
            var paramsToPass = methodParams
                .Select((p, i) => CreateParam(paramsOfDelegate, i, p, queueMissingParams))
                .ToArray();

            var expr = Expression.Lambda<T>(
                Expression.Call(method, paramsToPass),
                paramsOfDelegate);

            return expr.Compile();
        }
        else
        {
            var paramThis = Expression.Convert(paramsOfDelegate[0], method.DeclaringType);

            var paramsToPass = methodParams
                .Select((p, i) => CreateParam(paramsOfDelegate, i + 1, p, queueMissingParams))
                .ToArray();

            var expr = Expression.Lambda<T>(
                Expression.Call(paramThis, method, paramsToPass),
                paramsOfDelegate);

            return expr.Compile();
        }
    }

    private static Expression CreateParam(ParameterExpression[] paramsOfDelegate, int i, ParameterInfo callParamType, Queue<object> queueMissingParams)
    {
        if (i < paramsOfDelegate.Length)
            return Expression.Convert(paramsOfDelegate[i], callParamType.ParameterType);

        if (queueMissingParams.Count > 0)
            return Expression.Constant(queueMissingParams.Dequeue());

        if (callParamType.ParameterType.IsValueType)
            return Expression.Constant(Activator.CreateInstance(callParamType.ParameterType));

        return Expression.Constant(null);
    }
}

How it works

The core is the BuildDelegate method:

static T BuildDelegate<T>(MethodInfo method)

  • T is the delegate type you want to create.
  • method is the MethodInfo of the method you want to be called by the generated delegate.

Example call: var action = BuildDelegate<Action<object, int>>(mi);

Rules for parameters:

  • If the method passed is an instance method, first parameter of the generated delegate will accept the instance of the object, that contains the method itself. All other parameter will be passed to the method.

  • If the method passed is a static method, then all parameters of the generated delegate will be passed to the method.

  • Missing parameters will have default values passed.

  • Extra parameters will be discarded.
Miguel Angelo
  • 23,796
  • 16
  • 59
  • 82
  • I have detected that this answer does not support casting of the return value... that is, so that you can use `Func` with lets say an `int MyMethod()` for example... if you need it, feel free to ask, and I'll change it! =) – Miguel Angelo Nov 25 '12 at 23:55
  • I did not understand the logic in this line: Expression.Constant (Activator.CreateInstance (callParamType.ParameterType)); and in my tests it is never called .. When it is called? – J. Lennon Nov 26 '12 at 16:07
  • This is used to create a default object if it is not provided... e.g. you want an `Action` for a method like `void Method(int param1, int param2)`... note that param2 is not covered by the action, so the generated code must create a default value to pass to that method... it will call `Activator.CreateInstance(typeof(int))`, that is `0` (the default int value). – Miguel Angelo Nov 26 '12 at 17:03
  • Now, looking at it, it'd be better if it was possible to pass the default values for missing parameter on the `BuildDelegate` method... instead of just assuming you want to use the default value. – Miguel Angelo Nov 26 '12 at 17:06
  • 1
    Changed the answer to allow the `BuildDelegate` method to receive the default values of missing parameters. I also provided a sample, in the `Program.Main` method. – Miguel Angelo Nov 26 '12 at 17:50
  • There is a bug when using a static method which has return, you can check? – J. Lennon Jan 09 '13 at 12:18
7

Delegate.CreateDelegate is much simpler than building expression trees.

    var assembly = Assembly.GetAssembly(typeof(Foo));

    Type customType = assembly.GetType("Foo");

    var actionMethodInfo = customType.GetMethod("AnyMethod");

    var foo = Activator.CreateInstance(customType);

    Action action = (Action)Delegate.CreateDelegate(typeof(Action), foo, actionMethodInfo);
Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • 1
    The problem here is that you can not reuse the action, do this for all objects would be too laborious and costly. – J. Lennon Nov 14 '12 at 00:04
2

I've ran into a very similar scenario and I've found the following to work very well.

First, let's set up our test Foo class:

public class Foo
{
    private string _name;

    public Foo(string name)
    {
        _name = name;
    }

    public void AnyMethod()
    {
        Console.WriteLine("{0} Called: AnyMethod()", _name);
    }

    public void AnySetValue(string value)
    {
        Console.WriteLine("{0} Called: AnySetValue(string) with {1}", _name, value);
    }

    public string AnySetString(string value)
    {
        Console.WriteLine("{0} Called: AnySetString(string) with {1}", _name, value);
        return value;
    }
}

Next, we create a set of methods to create our reusable method wrappers:

public static Action<object> CreateReusableAction<TClass>(string methodName)
{
    var method = typeof(TClass).GetMethod(methodName);
    var del = Delegate.CreateDelegate(typeof(Action<TClass>), method);
    Action<object> caller = (instance) => del.DynamicInvoke(instance);
    return caller;
}

public static Action<object, object> CreateReusableAction<TClass, TParam1>(string methodName)
{
    var method = typeof(TClass).GetMethod(methodName, new Type[] { typeof(TParam1) });
    var del = Delegate.CreateDelegate(typeof(Action<TClass, TParam1>), method);
    Action<object, object> caller = (instance, param) => del.DynamicInvoke(instance, param);
    return caller;
}

public static Func<object, object, object> CreateReusableFunction<TClass, TParam1, TReturn>(string methodName)
{
    var method = typeof(TClass).GetMethod(methodName, new Type[] { typeof(TParam1) });
    var del = Delegate.CreateDelegate(typeof(Func<TClass, TParam1, TReturn>), method);
    Func<object, object, object> caller = (instance, param) => (TReturn)del.DynamicInvoke(instance, param);
    return caller;
}

Then we can use it as follows:

var myFoo = new Foo("myFoo");
var otherFoo = new Foo("otherFoo");

var anyMethod = CreateReusableAction<Foo>("AnyMethod");
anyMethod(myFoo);
anyMethod(otherFoo);

var anySetValue = CreateReusableAction<Foo, string>("AnySetValue");
anySetValue(myFoo, "Value 1");
anySetValue(otherFoo, "Value 2");

var anySetString = CreateReusableFunction<Foo, string, string>("AnySetString");
var firstResult = anySetString(myFoo, "MyFooValue1");
var secondResult = anySetString(otherFoo, "OtherFooValue1");

Which produces the output:

myFoo Called: AnyMethod()
otherFoo Called: AnyMethod()
myFoo Called: AnySetValue(string) with Value 1
otherFoo Called: AnySetValue(string) with Value 2
myFoo Called: AnySetString(string) with MyFooValue1
otherFoo Called: AnySetString(string) with OtherFooValue1
armen.shimoon
  • 6,303
  • 24
  • 32
  • 1
    seems good enough. Making some adjustments would leave everything in one method only. Not sure but using DynamicInvoke, there is a slight loss of performance. Anyway thank you for contributing. – J. Lennon Nov 25 '12 at 21:56
  • 1
    @J.Lennon If you're looking for much better performance, you'd need to replace the dynamic invokes with either an expression or emitting IL, which is surprisingly easier than one would expect. – armen.shimoon Nov 25 '12 at 22:31
2

If you can use libraries in your project, try the Impromptu Interfaces (available here: http://code.google.com/p/impromptu-interface/) or on nuget.

This has a lot of functionality for handling reflection on types and also implements internal caching. I've been using it in a project that relies heavily on reflection and the performance is really good.

The library itself has a lot of functionality but it also handles scenarios like yours.

byteflux
  • 226
  • 1
  • 6
0

I'm a bit unsure what you are really trying to do, but maybe this helps?

    public static Func<object, object[], object> CreateDelegate(MethodInfo method)
    {
        return new Func<object, object[], object>((object instance, object[] args) => method.Invoke(instance, args));
    }
    // Merely providing syntactic suger, ie able to write 
    // method.MyInvoke(instance, arg1, arg2, arg3, arg4) 
    // instead of having to put the args in an array, ie Invoke(instance, new object[]{arg1, arg2}) etc
    public static object MyInvoke(this Func<object, object[], object> func, object instance, params object[] args)
    {
        return func(instance, args);
    }

    public static void TestCode()
    {
        var method = typeof(string).GetMethod("get_Length");

        var wrappfunc = CreateDelegate(method);
        // Calling get_Length (ie Length property) on the string "klasjf"
        wrappfunc.MyInvoke("klasjf"); 
    }

You will lose the strong typing though, since all the arguments are object, and I am a bit unsure what you really are trying to accomplish.

flindeberg
  • 4,887
  • 1
  • 24
  • 37
  • 2
    Your code works perfectly, but is not suitable. But the problem is using call MethodInfo.Invoke is heavy and slow. call using a delegate or IL or an expression tree is much more performant. I will add information to my question.. – J. Lennon Nov 21 '12 at 09:59
0

I found a simple way to to do this for my ICommand factory I'm building:

public virtual ObjectCommand FromMethodName(string methodName)
{
    var t = typeof(T); // Inherited from generic class this factory resides within
    var m = t.GetMethod(methodName, Array.Empty<Type>());
    Action a = new Action(() =>
       {
          m.Invoke(ViewModel.ObjectModel, null); // Invoke the method name against the ViewModel's current object
       });
    return new ObjectCommand(this.ViewModel, a);
}

you can then create the action using the following, even works against a null object reference:

TestCommand = this.CommandFactory.FromMethodName(nameof(this.ObjectModel.SetResult));

As the reference to the ObjectModel changed, the result will change as well since its being invoked against the current reference

RFBomb
  • 51
  • 6
-1

I'm not sure, but why you don't use the dynamic type? For example:

dynamic foo = Activator.CreateInstance(customType);
foo.AnyMethod();
foo.AnySetValue("test string");
foo.OtherMethod("method", "with", "many", "parameters");
Vladimir
  • 39
  • 2
  • this works well for most cases, but if I need to change the method name (for example), I do not know a way to dynamically call this mode. – J. Lennon Oct 24 '12 at 09:51