using attributes in C#

  • Thread starter Thread starter raju
  • Start date Start date
R

raju

Hai,

I am new to Attributes in C#.

can anybody, explain how to use already created attributes in its
child class.

Thanks and Regards,
Raj.
 
raju said:
Hai,

I am new to Attributes in C#.

can anybody, explain how to use already created attributes in its
child class.

Thanks and Regards,
Raj.

The question is a bit vague, but I'll post my interpretation of it, and
the answer I can give. If the interpretation doesn't fit what you meant,
please elaborate.

I assume you mean that if you in a base class Base, tag, say, a property
with an attribute, and then in a descendant class wants to find out if a
property has been tagged with the attribute or not.

When you have, say, a PropertyInfo object, and call .IsDefined or
..GetCustomAttributes, there is a second boolean parameter that specifies
wether to include attributes inherited from a base class. If you specify
true here, you'll get inherited attributes.

However, some attributes are marked as non-inheritable, which means
they'll only appear if you scan an instance of the base class itself.

The purpose of these is typically for class attributes, where it isn't
obvious or even beneficial that a descendant automatically inherits the
attribute, and must in these cases be reapplied to the descendant class
as well.

One such example would be the SerializableAttribute, where a class
specifically needs to say it supports serialization, and can't rely on a
base class having said so once and for all.
 
Can you clarify a bit? i.e. are you trying do declare an attribute (and
subclassed attribute), or are you trying to write a standard class/struct
and *use* the existing attributes somehow?

If you could post an example of what you want to do (even if it doesn't
compile/work) it would probably help.

Marc
 
Back
Top