I have never posted in a code forum before, but I am at the end of my rope here...I apologize in advance for the lengthy code sample, but I tried to trim it down:
I have some C# classes that are generated from a schema using the XSD tool. I extend some of the types generated. I have one particular type that has a public ulong property. I am focusing on that property, because the only way I can get rid of the reflection error is if that property is a string instead of another type such as ulong or double, etc. In my example, I include another class that has a string property instead.
The strange thing is that I only get the reflection error when I try to derive my extended type from another class. However, there is no problem if the (derived) type has a string property instead of a ulong. I can however derive from an interface, but that does not suit my purpose. Currently, the only solution I have is to duplicate the code that would be in the base class if I could derive all my types.
The hierachy of my xml is this:
Root->Node1->definitions->variables->[variableBinary|variableDecimal]
As you can see from the example, the only substancial difference between variableBinaryType and variableDecimalType is the public Value property type. The base class from which I am deriving does absolutely nothing in this example. I have tried various attributes on the base class such as [Serializable()], and added an empty constructor, but I still get the reflection error. The only way to avoid it is if variableDecimalType is NOT derived from ANY class
Since the example is so lengthy, please download the zipped project from http://downloads.kinghomesite.com/Test.zip
Here is the really strange part. If you run the example with variableDecimalType actually derived from baseClass, the inner exception says, "There was an error reflecting property 'definitions'.", but I can solve the problem by NOT deriving variableDecimalType, whereas the xml hierachy is Root->Node1->definitions->variables->[variableBinary|variableDecimal].
Why doesn't the inner exception give me more information about what is wrong with variableDecimalType??
Thanks in advance,
sking
RussellPosted Sep 17, 2010, 3:54 PM
Stephen KingPosted Apr 3, 2009, 10:14 AM
I’m sorry, I guess one would be a little leery to download some strange .ZIP file from some strange site.
Let me try to exemplify it a little better here, plus I have some new information about the InnerException.
The class that I am trying to serialize is this:
public partial class variableDecimalType
{
private string nameField;
private byte bitSizeField;
private ulong valueField;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get { return this.nameField; }
set { this.nameField = value; }
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte bitSize
{
get { return this.bitSizeField; }
set { this.bitSizeField = value; }
}
[System.Xml.Serialization.XmlTextAttribute()]
public ulong Value
{
get { return this.valueField; }
set { this.valueField = value; }
}
}
Here is how I’m trying to extend the class:
public class baseClass
{
}
// You can comment out " : baseClass" and everything is fine!!??
public partial class variableDecimalType : baseClass
{
override public string ToString()
{
return this.name;
}
}
Here’s is how I’m serializing the object:
XmlSerializer serializer = new XmlSerializer(root.Node1.GetType());
root.Node1 is actually the great-grandparent of variableDecimalType…that is why I have to drill down to the inner-most exception to see the “real” exception as noted below. One could serialize this way as well:
XmlSerializer serializer = new XmlSerializer(typeof(variableDecimalType));
…then the exception I would be looking for is the first InnerException.
Now, the confusion on my part was that I was trying to use the Exception Assistant to see all the InnerExceptions, but the Exception Assistant is limited, so I could not drill down to the inner-most exception. When I did look at the inner-most exception, it is this:
“Cannot serialize object of type 'Test.variableDecimalType'. Consider changing type of XmlText member 'Test.variableDecimalType.Value' from System.UInt64 to string or string array.”
So does anyone know why I can’t have a ulong member, but only when I try to derive the class from another class? I can do one of 2 things:
1) Change the Value member to a string
2) Do not derive variableDecimalType from any other class.
This class will serialize as is with no problem if it is not derived from any other class.
Why can’t I derive it without changing the XmlText member to a string?