0

I am studying about attributes. I am new in them. I have a query. I have made some Custom Attribute class of my own. let me show some code.

public class MySpecialAttribute : Attribute
{
        public string Name { get; set; }

        public int Age { get; set; }
}

I have used this attribute on some methods.

public class UseOfAttribute
{
        [MySpecial]
        public void Hello()
        {
            Console.WriteLine("Hello");
        }

        [MySpecial]
        public void Bye()
        {
            Console.WriteLine("Bye");
        }

        public void TC()
        {
            Console.WriteLine("TC");
        }
}

Now my question is that I fetch all the methods with MySpecialAttribute at runtime and Can I change Name and age property at runtime. If I can do this then what is the way?

class Program
    {
        static void Main(string[] args)
        {
            var allMethods = typeof(UseOfAttribute).GetRuntimeMethods().Where(r => r.IsDefined(typeof(MySpecialAttribute))).ToList();            
        }
    }

This is the way of fetching my methods. Can anyone tell me? Thanks in advance.

adarsh
  • 31
  • 6
  • Have you checked this- https://stackoverflow.com/questions/12970353/c-sharp-dynamically-set-property? – Souvik Ghosh May 21 '19 at 13:28
  • You should look at this solution: https://stackoverflow.com/questions/6637679/reflection-get-attribute-name-and-value-on-property instead of reading the property you could write to it as well – Arne Klein May 21 '19 at 13:36

0 Answers0