Forum
 
ForumForumDiscussions and...Discussions and....Net framework ....Net framework ....NET Attributes - What applies to what?.NET Attributes - What applies to what?
Previous Previous
 
Next Next
New Post
 30/05/2008 13:44
 
 Modified By James  on 30/05/2008 13:45:53

HI,

I'm slowly discovering that a huge about of 'off the shelf' functionality in .NET is achievable by using the in built attributes... Making a user control behave as a container for other controls for example (code stolen from MS web site):

[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public class UserControl1 : System.Windows.Forms.UserControl
{
      ...
}

However, one of the things I am puzzling over (and some of my colleagues here too) is if there is an easy way to tell what attributes a class utilises because whilst any attribute can be applied to a class this is of course only useful if the class uses it. My expectation would be that the MS documentation for its classes would describe this but I cant seem to find that detail...

Any suggestions ??

New Post
 03/06/2008 20:28
 

Hi James,

That is a really interesting question.  Sadly it does not have a simple straightforward answer.  When an attribute is applied to a class then any code that can get hold of the type's Type object can interrogate it for custom attributes. 

It is certainly possible for a type to have its behaviour configured by applying attributes.  I suspect that is the less common use of attributes.  When done I would expect the class to document what attributes it responds to.

I suspect that the most common use of attributes is to configure code that uses the type rather then the type itself.  As an example; when you apply the Serializable attribute to one of your types it is very unusual (like - I have never done it) for the a type to use that itself.  It's used by the serialization mechanism.  So adding the Serializable attribute to your type effectively gives the serialization classes permission to poke around inside the non-public data of your type.

Your example of the Designer attribute is similar.  I doubt it is used by the control code.  It's more likely that Visual Studio (or some other tool) pokes around looking for the Designer attribute to work out how to display it at design time.  I suspect that to be a container control you should be inheriting from ContainerControl.   Check out http://msdn.microsoft.com/en-us/library/system.componentmodel.designerattribute.aspx.

 


Cheers,
- Richard
If this post helped you over a problem, or taught you something new, please login and rate it. Ratings are in the drop down in the top left corner
Previous Previous
 
Next Next
ForumForumDiscussions and...Discussions and....Net framework ....Net framework ....NET Attributes - What applies to what?.NET Attributes - What applies to what?

Forum Usage Guidelines

The forums are a place for all to exchange ideas and techniques, and to post and answer questions.  All are welcome to read, registration is required to post. 

If you learn somthing new, discover or acquire a new technique, then please take a moment to register and rate the post that just helped you.  This site does not send spam and it does not release your personal details.  Full details in the site privacy policy.

We have some simple posting guidelines to keep the forums a pleasant and informative environment.

  • No flames, no trolls
  • No profanity, no racism
  • Site management has the final word on approving or removing any thread, post, or comment
  • English language only please

 

Copyright 2002-15 by Dynamisys Ltd