"dickster" <gd***@kerrhenderson.com> wrote in message news:11*********************@o13g2000cwo.googlegro ups.com...
Why to i have to capatalise the first letter in ElementName & Type when
: : This doesnt compile
[XmlElementAttribute(elementName="dickster",type =
typeof(dickstersClass))]
But this does compile
[XmlElementAttribute(ElementName="dickster",Type =
typeof(dickstersClass))]
There is a fundamental difference between [XmlElement( "Joe")] and
[XmlElement( ElementName = "Joe")].
The first case is creating the XmlElementAttribute by calling the constructor
that takes a single string argument. As you've mentioned, the name of that
parameter is elementName, but it doesn't really -- the fact is it's a constructor
that takes one string and that's all the runtime cares about. The sequence of
arguments matters, because it must match the formal parameters list of the
constructor.
The second case creates the XmlElementAttribute by calling the default
constructor which takes no arguments. After it constructs the XmlElement-
Attribute, it then goes through and sets properties on it. The property name
is ElementName, proper case and case sensitive.
[XmlElement( ElementName = "Joe", Type = typeof( Restauranteur))], and
[XmlElement( Type = typeof( Restauranteur), ElementName = "Joe")]
are the same except for the order in which the properties get assigned. Thus,
it's not a magical way of passing parameters to a constructor where the run-
time magically figures out what you meant to do, but a standard feature that
is applicable by necessity.
Think of the requirements for XmlElementAttribute, and runtime attributes in
general, if there needed to be overloaded constructors for every possible
combination of properties developers might want to set? Without them, it
would be terribly inconvenient to set some of the less-frequently used
properties like IsNullable and Form (GetCustomAttributes( ) at run-time,
anyone?)
Derek Harmon