I spent the day calling potential clients in various public agencies, trying to establish some contacts. I like this part of my work. I enjoy selling Southwest Research Institute services and capabilities. Our organization has a lot of resources that enable us to carry out a lot of practical/applied R & D work.
I also spent some time honing my skills in class serialization techniques using C#. I ran into a gotcha, otherwise this would be a really boring blog entry. This is a code fragment from the XMLAttributeOverrides documentation that I was experimenting on:
/* Create an XmlElementAttribute to override the
field that returns Instrument objects. The overridden field
returns Brass objects instead. */
XmlElementAttribute attr = new XmlElementAttribute();
attr.ElementName = "Brass";
attr.Type = typeof(Brass);
// Add the element to the collection of elements.
attrs.XmlElements.Add(attr);
// Create the XmlAttributeOverrides object.
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
/* Add the type of the class that contains the overridden
member and the XmlAttributes to override it with to the
XmlAttributeOverrides object. */
attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
I modified the example while working with it, to include a second attribute named 'Tack':
/* Create an XmlElementAttribute to override the
field that returns Instrument objects. The overridden field
returns Brass objects instead. */
XmlElementAttribute attr = new XmlElementAttribute();
attr.ElementName = "Brass";
attr.Type = typeof(Brass);
// Add the element to the collection of elements.
attrs.XmlElements.Add(attr);
// Add a second attribute
attr.ElementName = "Tack";
attr.Type = typeof(Brass);
// Add the element to the collection of elements.
attrs.XmlElements.Add(attr);
// Create the XmlAttributeOverrides object.
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
/* Add the type of the class that contains the overridden
member and the XmlAttributes to override it with to the
XmlAttributeOverrides object. */
/*** Adding the second attr to attrs causes an error here ***/
attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
Unfortunately, it errored out when I attempted to add the attrs instance to the attrOverrides instance. I had to correct the code as follows:
/* Create an XmlElementAttribute to override the
field that returns Instrument objects. The overridden field
returns Brass objects instead. */
XmlElementAttribute attr = new XmlElementAttribute();
attr.ElementName = "Brass";
attr.Type = typeof(Brass);
// Add the element to the collection of elements.
attrs.XmlElements.Add(attr);
// Recreate the attr instance
attr = new XmlElementAttribute();
// Add a second attribute
attr.ElementName = "Tack";
attr.Type = typeof(Brass);
// Add the element to the collection of elements.
attrs.XmlElements.Add(attr);
// Create the XmlAttributeOverrides object.
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
/* Add the type of the class that contains the overridden
member and the XmlAttributes to override it with to the
XmlAttributeOverrides object. */
/*** Runtime accepts attrs after the changes ***/
attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
What happened here? I looked at the documentation in C# and couldn't find anything wrong. Then I studied the C++/CLI version and saw that the Add method takes a reference argument, similar to a pointer in native C++. Ahhh. We have to create a new instance to pass different parameters to the collection. By simply changing the values and adding the same instance of attr to the attrs instance, I was using the same reference to point to two separate pieces of data. I may not be a C# programmer, but I do know this is a very bad idea and probably won't work. (Why won't it work? I doubt the Add method of attrs is copying the attr instance data to a new instance for safe keeping.) So I reinitialized attr to a new instance. It was wierd not having to delete the old attr instance. I instinctively entered delete attr; on my first try and that threw a compile error. In native C++, I would have had to manually delete the first instance or else a memory leak would have occurred.
I'd like to give a shout out to my nephew, Ryan Weber. He's had a great
summer slogging through Europe. He's a very talented young man and I am
very proud of him. He's an excellent writer and you'll enjoy his blog Webber On The Lamb.