【发布时间】:2013-02-20 19:03:56
【问题描述】:
我正在尝试构建一个字典,为类中的每个静态方法编制索引,以便可以使用字符串查找它们。我似乎无法找到一种方法来实际从 MethodInfo 中获取对该方法的引用。这可能吗?
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创建委托并将其存储到方法中。
标签: c# reflection