Custom attributes

  • Thread starter Thread starter Edward Diener
  • Start date Start date
E

Edward Diener

I understand the syntax of custom attributes, but I have no idea what they
are supposed to do. Anyone care to give me a clue as to their functionality
?
 
Edward Diener said:
I understand the syntax of custom attributes, but I have no idea what they
are supposed to do. Anyone care to give me a clue as to their functionality
?

An attribute can add functionality or constraints to the attributed
"target". ManagedXLL for example provides a custom [WorksheetFunction]
attribute, that enables the the attributed method to be called from Excel
worksheets without modifying the method itself.

Browse the .NET class library and you will find lots of attributes and
examples.

Attributes also replace spearate IDL files in VC7.


Jens.
 
Jens said:
Edward Diener said:
I understand the syntax of custom attributes, but I have no idea
what they are supposed to do. Anyone care to give me a clue as to
their functionality ?

An attribute can add functionality or constraints to the attributed
"target". ManagedXLL for example provides a custom [WorksheetFunction]
attribute, that enables the the attributed method to be called from
Excel worksheets without modifying the method itself.

How does a custom attribute change usage of a particular type in MC++ ? Does
it direct the compiler to act differently ? That would seem to be impossible
since the compiler can not know about custom attributes. Does it allow the
programmer at run-time to react to a particular custom attribute by querying
the type for its attributes ? That would seem more probable but I am looking
for a practical example in MC++ code where this occurs.

In other words, let's say I create a custom attribute and use it for a
certain MC++ type. How practically would another programmer use my custom
attribute for added functionality when accessing an object of that type at
run-time ?
 
Edward Diener said:
Jens said:
Edward Diener said:
I understand the syntax of custom attributes, but I have no idea
what they are supposed to do. Anyone care to give me a clue as to
their functionality ?

An attribute can add functionality or constraints to the attributed
"target". ManagedXLL for example provides a custom [WorksheetFunction]
attribute, that enables the the attributed method to be called from
Excel worksheets without modifying the method itself.

How does a custom attribute change usage of a particular type in MC++ ? Does
it direct the compiler to act differently ?

No. Attributes are usually inspected by some kind of "container" that know
about your custom attribute.
Does it allow the
programmer at run-time to react to a particular custom attribute by querying
the type for its attributes ? That would seem more probable but I am looking
for a practical example in MC++ code where this occurs.

Yes, a custom attribute is usually read through reflecting on the target.
See ICustomAttributeProvider.
In other words, let's say I create a custom attribute and use it for a
certain MC++ type. How practically would another programmer use my custom
attribute for added functionality when accessing an object of that type at
run-time ?

I suggest that you lookup "Extending Metadata Using Attributes" on MSDN or
have a look at the classes derived from System.Attribute. There are lots of
examples.


Jens.
 
Back
Top