XmlSerializer and DefaultValue?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hello,

I'm currently playing around with the XmlSerializer to replace my old
handmade Xml parsing code, but am having a few small issues with
default values and derived classes.

Say we have classes A, B and C:

abstract class A
{
protected int a_value = 1;

[XmlAttribute("value"), DefaultValue(1)]
public int AValue
{
get { return a_value; }
set { a_value = value; }
}
}

class B : A
{
....
}

class C : A
{
....
}


Now, I want class B and C to have different default values, more
precisely, I want B to have 1 as default value and C to have
Int32.MaxValue. But I can't figure out how to actually make this work.
I tried making the property in A abstract and provide the
implementation in B and C with different default values, but it still
used the default value from A. I tried removing the default value from
A, which gave the result of having no default value, everything was
printed, always.

I then tried to remove the XmlAttribute attribute from A and only have
it and the default value in B and C, but it gave me the following
exception:

System.InvalidOperationException: There was an error reflecting
property 'AValue'. ---> System.InvalidOperationException: Member
B.AValue' hides inherited member 'A.AValue', but has different custom
attributes.


Anyone have any ideas for this? :-)

Thanks,
- Rasmus.

.



Relevant Pages

  • Re: Abstract class or interface?
    ... I try to make the decision based on the relationship of the derived class to the base class. ... If the derived class "can act like" the type of the base class, I'd lean towards an interface. ... I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: XmlSerializer and DefaultValue?
    ... have been searching for a solution, but yours is the only posting I've ... I'm currently playing around with the XmlSerializer to replace my old ... public int AValue ...
    (microsoft.public.dotnet.xml)
  • Re: array, xmlserializer and inheritance
    ... This is very similar to the Registry pattern ... > // This list will contain objects of derived classes - like B ... > I need to serialize class B with XmlSerializer but in the class A i ...
    (microsoft.public.dotnet.framework)
  • Re: Abstract class or interface?
    ... > I'm still trying to figure out concrete reasons to use one over the other. ... > in its methods and derived classes can only inherit one abstract class. ... The interface properties/methods have no implementation. ...
    (microsoft.public.dotnet.languages.csharp)
  • array, xmlserializer and inheritance
    ... // This list will contain objects of derived classes - like B ... cannot define what type will go in the List (with XmlArrayItem) because ... That's me favorite shirt. ...
    (microsoft.public.dotnet.framework)