Custom Attributes - Are they implicitly inherited by subclasses?

  • Thread starter Thread starter Lee
  • Start date Start date
L

Lee

public class ClassA
{
[MyCoolAttribute]
private string _FieldName;
public string FieldName
{
get { return _FieldName; }
}
}

public class ClassB: ClassA
{
// [MyCoolAttribute]
// Must be explicity typed in attribute or implicit in
// the base class?
private string _FieldName;
public string FieldName
{
get { return _FieldName; }
}
}

If ClassA implments a custom attribute on one of it's members, do
derived classes automatically get that attribute or must it be
explicitly added?

Thank you,

--
Warm Regards,
Lee

"Upon further investigation it appears that your software is missing
just one thing. It definitely needs more cow bell..."
 
public class ClassA
{
[MyCoolAttribute]
private string _FieldName;
public string FieldName
{
get { return _FieldName; }
}
}

public class ClassB: ClassA
{
// [MyCoolAttribute]
// Must be explicity typed in attribute or implicit in
// the base class?
private string _FieldName;
public string FieldName
{
get { return _FieldName; }
}
}

Since the base FieldName property isn't virtual you're shadowing the
base property with this new one. They are therefore different members
and no attributes are inherited.

If ClassA implments a custom attribute on one of it's members, do
derived classes automatically get that attribute or must it be
explicitly added?

In general, it depends on the AttributeUsageAttribute.Inherited
setting on the attribute class and how you call GetCustomAttributes.


Mattias
 
Back
Top