Inherit attribute with interface ?

  • Thread starter Thread starter TruongLapVi
  • Start date Start date
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.
 
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
 
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
 
Back
Top