Custom Attributes - Are they implicitly inherited by subclasses?

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..."
 
M

Mattias Sjögren

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
 

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