C# Useful Attributes

Attributes are a great tool in any C# developers toolbox. I confess I don’t use them enough and often struggle to remember their names. The short list below are attributes that I’ve found useful, including a few examples of their common uses. I’ll add more as I discover them. A list of MSDN pages on the subject can be found at the bottom of the post.

Updated: 27/Jun/2010

[AssemblyConfigurationAttribute]
Specifies the build configuration for the assembly.

[AssemblyTitleAttribute]
Specifies the assemblies description.

[Browsable]
Marks a custom control’s property as visible to the Visual Studio property grid.

[Browsable]
public Color HighlightColor { get; set; }

[Conditional]
Marks a method for inclusion in debug, but not release builds.

[Conditional("DEBUG")]
private void DebugMethod()
{
  ...
}

[Microsoft.SqlServer.Server.SqlFunction]
Marks a method of a user-defined aggregate as a function in SQL Server.

[ObsoleteAttribute]
Marks the program element as no longer in use.

[ObsoleteAttribute("This method is deprecated.")]
public void DeleteInvoice(int invoiceNo)
{
  ...
}

[PrincipalPermission]
Demands that the executing principle has the specified role.

[PrincipalPermission(SecurityAction.Demand, Role="Administrator")]
public void DeleteInvoice(int invoiceNo)
{
  ...
}

[WebService]
Defines the namespace and description for an Xml Web Service.

[WebMethod]
Exposes the specified method as part of an Xml Web Service.

[XmlAttribute]
Specifies how the XmlSerializer serializes a public field or public read/write property as an XML attribute.

[XmlArray]
Specifies how the XmlSerializer serializes a public field or read/write property that returns an array.

[XmlElement]
Used during object serialization. By setting ElementName property of this attribute, you can give a serialized object attribute an alternate name to its default.

[XmlElement(ElementName = "Premium")]
public int ReturnPremium;

[XmlIgnore]
Specifies whether or not the XmlSerializer serializes a public field or public read/write property.

[XmlRoot]
Specifies how the XmlSerializer serializes a class as an XML root element.

[XmlElement(ElementName = "Premium")]
public int ReturnPremium;

[XmlText]
Instructs the XmlSerializer to serialize a public field or public read/write property as XML text.

Useful Links

This is only the tip of the iceberg. More information can be found on the following websites:
Attributes That Control XML Serialization
Common Attributes (C# Programming Guide)
Extending Metadata Using Attributes
XmlAttributes Properties

Advertisements
This entry was posted in Reference and tagged . Bookmark the permalink.

2 Responses to C# Useful Attributes

  1. blacki says:

    No matter what others say, I think it is still interesting and useful maybe necessary to improve some minor things

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s