Consider the following overly simplified chunk of XML:
<ElementA>
<AttributeValue DataType="http://www.w3.org/2001/XMLSchema#integer">5
</AttributeValue>
</ElementA>
Specifically, looking at the AttributeValue element, from the DataType attribute, I know that my value is of type integer (though it could have been a double, string, datetime…any established datatype from the w3 standard). I would like to deserialize this xml to a .NET class with the strongly typed value. The first thing that came to my mind is to create a generic AttributeValue class:
public class AttributeValue<T>
{
public T Value {get; set;}
}
but of course this won’t work for a couple of reasons – the biggest one being I would have to declare the type in the parent class which won’t compile because T is not defined:
public class ElementA
{
public AttributeValue<T> {get; set; } // Naturally, this will not work because T
} // is not defined.
Plus, I would likely have to implement IXmlSerializable on my class to handle the custom serialization.
Is there a better way to solve this problem? I know I can serialize the DataType attribute in my code and store the value as a string, then convert later, but it would be helpful to actually have the correct type in my business object for later processing
Thanks for any help!
Jason
I appreciate your answer @caesay and I did implement it, but I’m not sure I require that type of functionality (to be able to add multiple properties to the dictionary). While I do use dynamo in dire need in my code, I try to avoid it where possible.
Instead, I implemented the following structure to retain the generic type within my parent class:
And a corresponding factory class to create the attribute value:
I hate to answer my own question on stackoverflow, but sometimes it happens.
Thanks for the responses guys!