Inherit attribute with interface ?

T

TruongLapVi

Hi everybody,

I have write customize attribute TestAttribute

[AttributeUsage(AttributeTargets.All, Inherited = true)]
public class TestAttribute : Attribute
{
private string name;
public TestAttribute(string name)
{
this.name = name;
}
}

[TestAttribute("Test test")]
public interface TestInterface
{
void function();
}

public class TestClass : TestInterface
{
public void function()...
}

But why TestClass does not inherit TestAttribute from TestInterface ???

Type t = typeof(DerivedClass);

TestAttribute testAttr = (TestAttribute)Attribute.GetCustomAttribute(t,
typeof(TestAttribute));

testAttr alway null. Can anybody help me ?
Thanks.
 
V

Vijaye Raji

Did you try

TestAttribute testAttr = (TestAttribute)Attribute.GetCustomAttribute(t,
typeof(TestAttribute), true);

[Note the "true" at the end of the call]

The true tells the method to look for this attribute in the full inheritance
chain.

-vJ
 
1

100

Hi TruongLap,
It seems like attributes cannot be inherited from interfaces. If you try and
make TestInterface class instead of interface you will find out that
everything works how it suppose to.

B\rgds
100
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top