views:

167

answers:

2

I'm trying to generate code from an XSD that has two complexTypes extending a common complexType. The inheritors have a common property named "value", which has different types. Of course I can't put it on my base type due to XSD rules. My intention is to polymorphically call the property on the base type which would invoke the right method on the subclass. Is this possible?

Here is my XSD

<xs:complexType name="Property">
 <xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="StringProperty">
 <xs:complexContent>
  <xs:extension base="Property">
   <xs:attribute name="value" type="xs:string" use="required"/>
  </xs:extension>
 </xs:complexContent>
</xs:complexType>
<xs:complexType name="BooleanProperty">
 <xs:complexContent>
  <xs:extension base="Property">
   <xs:attribute name="value" type="xs:boolean" use="required"/>
  </xs:extension>
 </xs:complexContent>
</xs:complexType>

This generates the following code:

public partial class Property {

    private string nameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }
}

public partial class StringProperty : Property {

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

public partial class BoolProperty : Property {

    private bool valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

And here's what I want to do:

            // ConfigProperties is an array of Property objects
            foreach (Property property in config.ConfigProperties)
            {
                // Doesn't compile since Property.value doesn't exist in the base class.
                Console.WriteLine(property.value);
            }

I tried implementing a "value" getter property on the Property object hoping the subclasses' "value" property definitions would hide it, but that doesn't do the trick:

public partial class Property
{
    public virtual string value
    {
        get { throw new NotImplementedException(); } // this method is called instead of the Property subclass methods
    }
}

FOLLOW-UP QUESTION EDIT: The answers confirm my fears that this is not possible. Would it still be possible though to do the following in some manner:

            foreach (Property property in config.ConfigProperties)
            {
                somevalue = property.GetValue();
            }

where GetValue() is a method that figures out what return type it should have by virtue of the subclass of Property it actually is using? Pardon me for being extremely lazy. I'm given to believe that it's a virtue. =)

+1  A: 

Your are deserializing into Property objects, therefore the serializer creates objects of the type Property. You will not know at runtime whether you had a BoolProperty or a StringProperty.

You need to extract the StringProperties from the XML, deserialize them into StringProperty objects, and then the same for BoolProperties.

You can then combine them into one list of the type Property. The app will then behave like expected as long as you are creating a virtual property on your baseclass which you override in your implementation classes.


response to author's edit
It will not work, as you do not have the correct information at that point. You will have to explicitly deserialize your object as a StringProperty to have .NET recognize your property. You will never be able to detect the propertytype in your function.

If you have Property objects of the correct type, you can create an extension method to facilitate:

public string GetValue(this Property property)
{
    if(property is StringProperty) return ((StringProperty)property).Value;
    else if ...
}

Yet you need to know the correct type here, which you do not know if you are deserializing your entities as Property objects.

Jan Jongboom
I guessed as much. I think this would have been easier if there without the XSD limitations on extensions and overrides. Thanks Jan!
Jeremy
+1  A: 

I do not think is possible to do it the way you are trying to.

You could take advantage of the partial classes mechanism, though.

For example, you could add another file for the Property partial class and add a virtual method called something like "public virtual string ValueToString()", the override it in StringProperty and BoolProperty (also by means of partial classes mechanism). This way you would be able do make the foreach work (call property.ValueToString()).

Of course this means you must do custom work after generating the classes, but I do not see other ways.

Ciprian Bortos
Thanks for the lighting-fast replies! I have a follow-up question above though.
Jeremy