If I have a host of properties in an interface, but in the example I will just use one as it demonstrates what I am trying to achieve.
interface IFoo
{
[Bar()]
string A { get; set; }
}
class Base { }
class Foo : Base, IFoo
{
public string A { get; set; }
}
So when I do this:
Foo f = new Foo();
f.A = "Value";
Attribute b = Attribute.GetCustomAttribute(f.GetType().GetProperty("A"), typeof(Bar));
I was expecting to be able to get the instance of my Bar attribute out. Most of this is being done in a generic class and I am using my attributes for a validation model so I can’t implicitly cast to an interface then get the attribute of the property in the interface because I never know what type the interface will be or what type will implement it. I need some way of getting the attribute out of my instance of Base for example.
public void GenericMethod<T>(T instance) where T : Base
{
//Get my instance of Bar here.
}
I hope whatI am trying to do is clear, thanks in advance.
This will give you the list of all custom attributes
Barapplied to all properties in type ofinstance:Is that what you are looking for?