繁体   English   中英

如何从 MemberInfo 中获取 GetCustomAttribute 方法?

[英]How to get the method GetCustomAttribute from MemberInfo?

我尝试对某些类型进行一些反思。

所以我有这个

 public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        [MethodForRun(RunCount = 3)]
        public void Print()
        {
            Console.WriteLine($"{FirstName} {LastName}");
        }


        [MethodForRun(RunCount =2)]
        public void SayHello()
        {
            Console.WriteLine($"Hello THere {FirstName}");
        }

        static void AttributeTest(Type type)
        {
            var allMethods = type.GetMethods();
            var methodWithAttributes = allMethods.Where(x => x.GetCustomAttribute(typeof(MethodForRunAttribute)) != null);
        }
    }

    public class MethodForRunAttribute:Attribute
    {
        public int RunCount { get; set; }
    }

它是关于方法 AttributeTest 的。

我就是做不到

x.GetCustomAttribute

我只能做 GetCustomAttributes - 所以复数。 但不仅仅是 GetCustomAttribute。

rror    CS1061  'MethodInfo' does not contain a definition for 'GetCustomAttribute' and no accessible extension method 'GetCustomAttribute' accepting a first argument of type 'MethodInfo' could be found

我必须改变什么?

谢谢

还有图书馆

??

using System.Reflection;

我有这个命名空间。 但它没有使用它。 于是我又加了。 现在它可以工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM