locked
Extending auto-generated serializable objects RRS feed

  • Question

  • I used xsd.exe to generate class files from an XSD so that I can serialize and deserialize XML. Now, I want to extend the default classes so I can add functionality to them without interfering with the automatically generated code. I don't want to leverage the fact that they're partial classes because I want the base object to be distinct. The class I really want to extend is nested two levels deep within other auto-generated classes:

    ClassA  has instances of ClassB which has instances of ClassC.  I want to actually extend the functionality of ClassC.

    What I tried to do was create ClassA' which overrides references to ClassB with ClassB' and then ClassB' overrides references to ClassC with ClassC'. ClassC' extends ClassC and contains a few extra custom methods (the extended functionality).

    When I set ClassA' as my type and try to deserialize, it breaks with the following runtime exception:

    [InvalidOperationException: Member ClassB'.classC'Ref of type ClassC'[] hides base class member ClassB.classCRef of type ClassC[]. Use XmlElementAttribute or XmlAttributeAttribute to specify a new name.]

    I might be approaching this totally the wrong way so guidance to fix this or using a different approach are greatly appreciated!

    Friday, November 19, 2010 3:08 AM

All replies

  • ClassA  has instances of ClassB which has instances of ClassC.  I want to actually extend the functionality of ClassC.

    What I tried to do was create ClassA' which overrides references to ClassB with ClassB' and then ClassB' overrides references to ClassC with ClassC'. ClassC' extends ClassC and contains a few extra custom methods (the extended functionality).

    I have the feeling you're using some words in a wrong way.

    "ClassB has instances of ClassC" is totally irrelevant. It has no impact on extending the functionality of ClassC.

    You cannot "overrides references". You can override methods, properties and events.

    So, I'm not sure what exactly you're doing.

    Friday, November 19, 2010 5:04 PM