是否有任何类可以为您提供MethodInfo /方法名称?

时间:2013-05-18 13:10:02

标签: c# reflection compiler-construction .net-4.5 method-names

我想知道是否有任何类可以生成MethodBase / MethodInfo,或者只是生成方法名称,而不使用“魔术字符串”。现在我正在做以下事情:

public void Foo(MethodInfo method)
{
    if (String.Equals("get_IsBar", method.Name, StringComparison.Ordinal))
    {
        // ...
    }
}

相反,我想知道是否有某种方法可以从界面获取名称“get_IsBar”,如下所示:

public interface IBar
{
    bool IsBar { get; }
}

public void Foo(MethodInfo method)
{
    string barMethodName = GetBarGetterMethodName(typeof(IBar), "IsBar");
    if (String.Equals(barMethodName, method.Name, StringComparison.Ordinal))
    {
        // ...
    }
}

我意识到那里仍然有一个“神奇的字符串”,但至少它更容易管理。

1 个答案:

答案 0 :(得分:1)

使用GetGetMethod()的{​​{1}}:

PropertyInfo