Problem

I have a method:

public static Delegate MethodToDelegate(MethodInfo method) { Delegate d = ...; // использовать ТОЛЬКО параметр method и d return d; } 

How do i create a delegate d in this method. I know that you can use the CreateDelegate , but how do I know the type of the delegate?

    1 answer 1

     ParameterInfo[] pars = method.GetParameters(); List<Type> param = new List<Type>(); List<Type> methods = new List<Type>(); methods.Add(method.ReturnType); foreach (ParameterInfo p in pars) { param.Add(p.ParameterType); } Delegate d = method.CreateDelegate(Expression.GetDelegateType(param.Union(methods).ToArray())); return d; 

    But be careful the method passed must be static, and there must also be no IsGenericMethod

    • The Hard Way ... - NewView
    • @NewView And what is simple? - return
    • This is the right way, Pavel, I would say academic :) do this, this is the right way. - NewView 5:28 pm
    • @NewView I do not understand your irony? You do not like the way the parameters are passed? - Yury Bakharev
    • Where did you see the irony? I did not mean anything like that. - NewView 5:33 pm