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);
}
}
}
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
.
The whole point of this type of delegate-reflection optmization is that you know what type of delegate you need at the compile time. If you cast it to type Delegate
like this Delegate dlg =
you will have to invoke it with Invoke method which is the same reflection.
So you should use IL generation or Expression Trees instead to generate neutral delegates like Func<object,object[],object>
.
Also read this for better understanding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With