I have created a custom attribute that I am using on my class MyClass. I would like to be able to reflect on an instance of this class to see information from that attribute.
Let’s say I have this class definition:
public class MyClass
{
[Example("MyClassID")]
public string ID;
}
Later in my code, I am using an instance of this class:
MyClass mc = new MyClass();
mc.ID = 18;
Is there a way I can get the attribute value (“MyClassID”) from this instance, mc? Ideally, I’d like to be able to tie this to my property, in a way similar to this:
mc.ID.GetCustomAttributes(typeof(ExampleAttribute), false)
Yes, you can do this through reflection:
If this is not a property (which it isn’t in your example, but I wasn’t sure if that was a function of the question or not), then you can use GetMember.