System.Attribute access to target 
Author Message
 System.Attribute access to target

I am a bit confused about attributes in CSharp - if anyone could shed
some light on an issue I have...

Some attributes require no parameters on their constructors, but yet
somehow affect the behavior of the class or method to which they are
targeted.  In example, [WebMethod] causes a method of a WebService to
be exposed for use, but is not given any information about the method
(directly).

[AutoComplete] causes a call to SetComplete if the transaction
completes successfully, but may be instantiated with no constructor
parameters.

How do you design an Attribute that "knows" about the method or class
to which it is targeted?

I ask this because i would like to create an Attribute that, once
constructed, can interact with it's target (find the method & class
name, detect the user, etc...) specifying this information at design
time will not work for obvious reasons.

Any help would be appreciated - Thanks!



Mon, 01 Nov 2004 02:04:10 GMT  
 System.Attribute access to target
Barry,

    You can not really create attributes that interact with it's target, the
reason for this being that attributes are associated with the type of the
target, not with the instance of the target itself.

    Attributes are a way of specifying specialized metadata with the
metadata (type information) that your type (class) already declares for
itself.  When the assembly is compiled, the attribute information is
included with the type.  At runtime, when the type is reflected upon, the
attributes for that type (not instance) are created as well.  The
appropriate parameters for the constructor are then passed in for the type,
just like any other class.

    Now, you see attributes like AutoComplete, or WebMethod.  Frameworks
(such as EnterpriseServices or ASP.NET) reflect on type information to
determine what actions should be taken for particular types (for all
instances of the type).  If you want to make individual determinations on
actions for instances of types, then I would recommend using an interface on
the instances to get a type-safe way of getting instance-specific
information to make a determination on the course of action to take.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> I am a bit confused about attributes in csharp - if anyone could shed
> some light on an issue I have...

> Some attributes require no parameters on their constructors, but yet
> somehow affect the behavior of the class or method to which they are
> targeted.  In example, [WebMethod] causes a method of a WebService to
> be exposed for use, but is not given any information about the method
> (directly).

> [AutoComplete] causes a call to SetComplete if the transaction
> completes successfully, but may be instantiated with no constructor
> parameters.

> How do you design an Attribute that "knows" about the method or class
> to which it is targeted?

> I ask this because i would like to create an Attribute that, once
> constructed, can interact with it's target (find the method & class
> name, detect the user, etc...) specifying this information at design
> time will not work for obvious reasons.

> Any help would be appreciated - Thanks!



Mon, 01 Nov 2004 02:48:31 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Reflection: Attribute.Target

2. Creating custom attribute in assembly and accessing attribute from different project

3. Systems you target with C

4. Macro access to target name

5. Windows service applications (System.Diagnostics.DebuggableAttribute headaches)

6. Attributes, System Headers, and MIDL

7. Problem while accessing the attributes of a WebService

8. Accessing Assembly Attributes

9. SetFileTime last access attribute not working on a folder

10. Accessing network files attributes

11. Problem while accessing the attributes of a WebService

 

 
Powered by phpBB® Forum Software