.net Type.GetMethods的BindingFlags不包括属性访问器

.net Type.GetMethods的BindingFlags不包括属性访问器,.net,reflection,.net,Reflection,假设我有以下程序: namespace ReflectionTest { public class Example { private string field; public void MethodOne() { } public void MethodTwo() { } public string Property { get { return field; }

假设我有以下程序:

namespace ReflectionTest
{
    public class Example
    {
        private string field;

        public void MethodOne() { }

        public void MethodTwo() { }

        public string Property
        {
            get { return field; }
            set { this.field = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            iterate(typeof(Example));

            Console.ReadLine();
        }

        public static void iterate(Type type)
        {
            MethodInfo[] methods = type.GetMethods(
                BindingFlags.DeclaredOnly |
                BindingFlags.Instance |
                BindingFlags.Public);

            foreach (MethodInfo mi in methods)
            {
                Console.WriteLine(mi.Name);
            }
        }
    }
}
当我运行程序时,我得到以下输出:

MethodOne MethodTwo get_Property set_Property 你知道我应该使用什么
BindingFlags

更新: 我应该解释一下,这个项目实际上是为了自动构建单元测试的模板,所以我可以跳过所有的特殊方法。感谢您提供有关IsSpecialName:)的更多信息


林克?真正地哇!无论如何,这个项目是.NET 2.0,所以LINQ(很遗憾)不是一个选项。

从我的头脑中:

mi.IsSpecialName &&( mi.Name.StartsWith("set_") || mi.Name.StartsWith("get_"))
你应该准备好了。 SpecialName不仅仅是属性访问器(这里也包括事件添加/删除方法),这就是为什么您还必须检查名称的原因


您也可以使用LINQ:)

对于记录,答案有一个更简单的方法来实现这一点。@wdosanjos另一个答案还将返回其他特殊名称方法,如事件订阅/取消订阅
mi.IsSpecialName &&( mi.Name.StartsWith("set_") || mi.Name.StartsWith("get_"))