vendredi 24 décembre 2021

How to get the method GetCustomAttribute from MemberInfo?

I try to do some reflection on some types.

So I have this

 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; }
    }

And it is about the method AttributeTest.

I just can't do

x.GetCustomAttribute

I only can do GetCustomAttributes - so plural. But not just GetCustomAttribute.

What I have to change?

Thank you

And the Library





Aucun commentaire:

Enregistrer un commentaire