views:

69

answers:

1

I want to create a Delegate of a reflected method, but the Delegate.CreateDelegate requires the Type of delegate be specified. Is it possible to dynamically create a 'Delegate' that matches whatever function is reflected?

Here is a simple example:

class Functions
{
    public Functions()
    {

    }

    public double GPZeroParam()
    {
        return 0.0;
    }

    public double GPOneParam(double paramOne)
    {
        return paramOne;
    }

    public double GPTwoParam(double paramOne, double paramTwo)
    {
        return paramOne+paramTwo;
    }
}

static void Main(string[] args)
{
    Dictionary<int, List<Delegate>> reflectedDelegates = new Dictionary<int, List<Delegate>>();
    Functions fn = new Functions();
    Type typeFn = fn.GetType();
    MethodInfo[] methods = typeFn.GetMethods();

    foreach (MethodInfo method in methods)
    {
        if (method.Name.StartsWith("GP"))
        {
            ParameterInfo[] pi = method.GetParameters();

            if (!reflectedDelegates.ContainsKey(pi.Length))
            {
                reflectedDelegates.Add(pi.Length, new List<Delegate>());
            }

            // How can I define a delegate type for the reflected method at run time?
            Delegate dlg = Delegate.CreateDelegate(typeof(???), fn, method);
            reflectedDelegates[pi.Length].Add(dlg);
        }
    }
}

Update:

The closest thing I found is this FastInvokeWrapper on code-project, but I'm still trying to wrap my head around it and I don't quite understand how GetMethodInvoker binds the reflected method to the FastInvokeHandler.

+4  A: 

Take a look at Jon Skeet's blog entry, it handles this exact case. It's some pretty hairy code though (there are methods called "Magic"), but it's explained really well.

Matt Warren
@Matt, I'll try that...
Lirik