Attributes are widely used in the .NET Framework to annotate code. For example, XML serialization uses attributes to control how a class is serialized. The syntax of attributes can be confusing until you realize that attributes are actually just managed classes. This article provides a quick guide to understanding attribute syntax, how to read it, and how to look up attribute documentation to be able to specify them in your programming practice.
Attributes are used all over the place in .NET. Let us consider an XML serialization example where attributes take an important and necessary role. In this example, we will be designing a C# class and streaming it as XML. The C# class definition follows:
Public class MyClass
{
public string myField = "abc";
}
The above class can be streamed as XML as follows:
MyClass myObject = new MyClass;
XmlSerializer xs = new XmlSerializer(typeof(myObject));
StringWriter sw = new StringWriter();
Xs.serialze(sw,myObject);
This will produce an XML as follows:
<MyClass>
<myField>abc</myField>
</MyClass>
Let us now introduce attributes so that the generated XML can be tailored.
[XmlRoot("MyXmlClass")]
Public class MyClass
{
[XmlElement(ElementName="myXmlField")]
public string myField = "abc";
}
This will produce an XML as follows:
<MyXmlClass>
<myXmlField>abc</myXmlField>
</MyXmlClass>
|
Related Reading
|
There are many more attributes in XML serialization to control the serialization process. You can see these attributes in the references section of this article. But for now, these two attributes are sufficient for our discussion.
When I have encountered these attributes, my first problem was figuring
out how to read this attribute specification. What is the meaning of
"[XmlRoot("somename")]"? I could guess that the programmers were trying to set the
name of the root node of the XML, but the syntax is unclear. In one case, they are specifying a string directly inside quotes; in
another case, they are using some sort of a key to specify the string
against.
Sometimes they use commas to separate multiple attributes, and sometimes they stack them. Sometimes there are repeated attributes and sometimes not. Sometimes they seem to be applied to fields and sometimes to classes. It seemed to be a very inconsistent set of rules.
But it turns out there is a method to this madness, after all. The key to understanding attributes lies in the fact that they represent .NET classes (first-class citizens of the .NET world). Let us investigate this aspect of attributes:
XmlRoot
represent? This name is a short cut for System.Xml.Serialiation.XmlRootAttribute.
I will show you later how this long name shrinks to just XmlRoot.System.Xml.Serialization namespace, you can shorten
the name to XmlRootAttribute.[namespace.attributeNameAttribute](arg1,arg2,param1=value,param2=value);
[attributeNameAttribute](arg1,arg2,param1=value,param2=value);
[attributeName](arg1,arg2,param1=value,param2=value);
The first line above identifies a fully qualified class name of an attribute,
followed by the constructor arguments, followed by the public properties.
The second line omits the namespace from the fully qualified name. The
third line omits the Attribute suffix from the class name. This is
purely a convenience offered by the programming facilities. Let us see
some examples demonstrating these variations.
1. [System.Xml.Serialization.XmlRootAttribute(ElementName="LOAD_TENDER_TRIP",
Namespace="", IsNullable=false)]
2. [System.Xml.Serialization.XmlRootAttribute("LOAD_TENDER_TRIP",
Namespace="", IsNullable=false)]
3. [XmlRootAttribute(ElementName="LOAD_TENDER_TRIP",
Namespace="", IsNullable=false)]
4. [XmlRoot(ElementName="LOAD_TENDER_TRIP", Namespace="", IsNullable=false)]
using System.Xml.Serialization.Attribute suffix is omitted.We can summarize the various rules governing attributes like so:
[]) and are usually specified right
above the programming construct to which they belong. Attribute.Attribute
specified in rule 2.Attribute can be omitted from the specification. .NET
will add this suffix to locate the class. If the original class
does not have an Attribute suffix, then the name (as it is) is used to look for the class.[attribute1()] [attribute2()]
[attribute1(), attribute2()
[attribute1]
Attribute
and sans Namespace).Satya Komatineni is the CTO at Indent, Inc. and the author of Aspire, an open source web development RAD tool for J2EE/XML.
Return to ONDotnet.com
Copyright © 2009 O'Reilly Media, Inc.