4

I'm trying to build a dictionary that indexes each static method in a class so they can be looked up with a string. I can't seem to find a way to actually get a reference back to the method from the MethodInfo. Is this possible?

delegate void SkillEffect(BattleActor actor, BattleActor target);

public static class SkillEffectLookup
{
    public static Dictionary<string, SkillEffect> lookup;

    public static void Build()
    {
        lookup = new Dictionary<string, SkillEffect>();
        Type type = typeof(SkillEffects);
        var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public);
        foreach (MethodInfo methodInfo in methods)
        {
            lookup.Add(methodInfo.Name, _____________);
        }
    }

public static class SkillEffects
{
    public static Attack(BattleActor actor, BattleActor target)
    {
        // Do Things
    }

    public static NonAttack(BattleActor actor, BattleActor target)
    {
        // Do Other Things
    }
}
ustor
  • 43
  • 1
  • 3
  • I think you want to use the `Delegate.CreateDelegate` method. With that you can create and store the delegate to the methods using your `MethodInfo`. – Chris Sinclair Feb 20 '13 at 19:09
  • 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) – Rune FS Feb 20 '13 at 19:22

4 Answers4

3

Try to use CreateDelegate method. It will work only if you know method`s signature.

http://msdn.microsoft.com/en-us/library/system.delegate.createdelegate.aspx

UPD (tnx to Chris Sinclair):

example of using

lookup.Add(methodInfo.Name
      , (SkillEffect)Delegate.CreateDelegate(typeof(SkillEffect), methodInfo));
Oleh Nechytailo
  • 2,155
  • 17
  • 26
  • Maybe add the relevant line of code ustor is asking for: `lookup.Add(methodInfo.Name, (SkillEffect)Delegate.CreateDelegate(typeof(SkillEffect), methodInfo));` – Chris Sinclair Feb 20 '13 at 19:12
2

from the code it would seem you are looking for a delegate rather than a reference to a method. (Which does not really exist in C#)

I would change the dictionary to Dictionary<string,Func<BattleActor,BattleActor> lookup though this is a matter of personal preference and unrelated to your issue. (You can substitute Func<BattletActor,BattlActor> with SkillEffect in the below code)

and then do

Func<BattleActor,BattleActor> func = (Func<BattleActor,BattleActor>)
                     Delegate.CreateDelegate(typeof(Func<BattleActor,BattleActor>), methodInfo);

lookup.Add(methodInfo.Name,func);

A functor is a delegate and can be invoked just like any other delegate

 lookup["mymethod"](actor,target);

you could see this question for more information

Community
  • 1
  • 1
Rune FS
  • 21,497
  • 7
  • 62
  • 96
0

MethodInfo is the metadata for the method. To actually invoke the method you call (surprise!) MethodInfo.Invoke.

Does that answer your question?

0

If you're looking to save a reference to the method itself so you can call it, I don't think you can. Really what you do is call it via the Invoke method on the MethodInfo:

        foreach (MethodInfo methodInfo in methods)
        {
            lookup.Add(methodInfo.Name, _____________);
        }

And then to call it:

lookup[methodName].Invoke(null, BindingFlags.Public | BindingFlags.Static, null, args, null);
Shane Andrade
  • 2,655
  • 17
  • 20
  • Well technically there's no function point in C# (unless you get fancy with unsafe code) so no you can't but I think OP is actually talking about delegates and you can create delegates from MethodInfo objects – Rune FS Feb 20 '13 at 19:16