我試圖構建一個字典,它將一個類中的每個靜態方法編入索引,以便可以用字符串查找它們。我似乎無法找到一種方法來實際從MethodInfo中獲取參考。這可能嗎?如何從MethodInfo(C#)獲取對方法的引用
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
}
}
我想你想使用'Delegate.CreateDelegate'方法。有了這個,你可以使用你的'MethodInfo'創建和存儲委託給方法。 – 2013-02-20 19:09:57
[可以從MethodInfo對象獲得Func(或類似)嗎?](http://stackoverflow.com/questions/2933221/can-you-get-a-funct-or-similar-from- a-methodinfo-object) –
2013-02-20 19:22:52