3

I know how to get MethodInfo for a particular method, and also know how to call that method via reflection. However I could not figure out the following:

I have the assignment statement below:

Func<double, double> myFunc = Math.Sqrt;

I would like to get the exact same content for the myFunc variable via reflection, having the MethodInfo for Math.Sqrt in hand. Unfortunately building wrapper lambdas or Expression around the MethodInfo is not satisfying. I would like something like this:

Func<double, double> myFunc = GetMethodGroupFor(methodInfoForMathSqrt);

In case if the isolated sample does not explains what I would like to really do here is a bit more explanation:

I have to fill a Dictionary<string, Func<double, double>>with 100s of method "keys" and delegates. Like:

myDictionary.Add("mykey", Math.Sqrt);

However I would not like to do this by 100s of assignment statement, instead I would like get the 100s of appropriate MethodInfos via reflection then in a for cycle fill the dictionary.

Is this possible?

g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • so, you want get `MethodInfo` and save it in variable with type like `Func`? – Grundy Nov 02 '15 at 08:22
  • Possible duplicate of [Can you get a Func (or similar) from a MethodInfo object?](http://stackoverflow.com/questions/2933221/can-you-get-a-funct-or-similar-from-a-methodinfo-object) – Grundy Nov 02 '15 at 08:23
  • I would like to have the exact same content of the myFunc variable like in the code sample. I would like to do this via reflection – g.pickardou Nov 02 '15 at 08:24
  • see duplicated question – Grundy Nov 02 '15 at 08:25
  • Yes I've read it and the answers. Unfortunately the _similar_ is not suitable for me. (please see the similar word in that title). I've also have ideas about creating delegates and expressions, but that is unfortunately not satisfying.... – g.pickardou Nov 02 '15 at 08:27
  • so, what you really want and have? you provide some statement, but not explain what you get by reflection and what you get at end – Grundy Nov 02 '15 at 08:31
  • I think I've explained why the referred "duplicate" is not a duplicate both in comments and both in the edited question – g.pickardou Nov 02 '15 at 08:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93950/discussion-between-grundy-and-g-pickardou). – Grundy Nov 02 '15 at 08:31
  • 1
    With reflection you've got access to the name of those methods, and you can create delegates from method-info's via `Delegate.CreateDelegate`, so yes, this is perfectly possible. – Pieter Witvoet Nov 02 '15 at 08:46

1 Answers1

2

I assume you'll be using reflection to look for methods that match a particular signature as following:

IEnumerable<MethodInfo> methods = typeof(Math).GetMethods()
    .Where(method =>
    {
        if (method.ReturnType != typeof(double))
            return false;

        var parameters = method.GetParameters();
        return parameters.Length == 1 && parameters[0].ParameterType == typeof(double);
    });

From there, you can fill your dictionary as following:

var methodLookup = new Dictionary<string, Func<double, double>>();
foreach (MethodInfo method in methods)
{
    var name = method.DeclaringType.Name + "." + method.Name;
    var d = (Func<double, double>)Delegate.CreateDelegate(typeof(Func<double, double>), method);
    methodLookup[name] = d;
}
Pieter Witvoet
  • 2,773
  • 1
  • 22
  • 33