Bad example on my part as I used a simple type consisting of ints. I was
trying to keep is as simple as possible but that isn't going to work in this
case. Instead of using ints I have other custom classes which are defined
and returned. These custom classes are either redefined in the XSD or
extended in the XSD so the example holds true for what I was intending but
the delivery of the example was bad. The intended purpose is to hide the
base Implementation of ObjectA (thus the new keyword) and replace it with
ObjectB which is defined to extend ObjectA while at the same time reusing
the XmlElement name.
So far from what I can tell it appears that I can't hide a base XmlElement
and might have to use an XmlAttributes and XmlAttributeOverrides, but I'm
not seeing how this works with an XmlElement only XmlAttributes, XmlEnums,
etc....
namespace A
{
[XmlRoot("Foo")]
public class BaseClass
{
private ObjectA miCount;
[XmlElement("Count", Namespace="BaseClass")]
public ObjectA Count
{
get {return miCount;}
set {miCount = value;}
}
}
public class ObjectA
{
[XmlAttribute("FavNum")]
public int FavoriteNum
{
get;
set;
}
}
}
namespace B
{
[XmlRoot("Bar")]
public class SubClass : A.BaseClass
{
private ObjectB miCount;
[XmlElement("Count", Namespace="SubClass")]
public new ObjectB Count
{
get {return miCount;}
set {miCount = value;}
}
}
public class ObjectB : A.ObjectA
{
[XmlAttribute("FavLetter")]
public int FavoriteLetter
{
get;
set;
}
}
}
> Bad example on my part as I used a simple type consisting of ints. I was
> trying to keep is as simple as possible but that isn't going to work in
[quoted text clipped - 5 lines]
> replace it with ObjectB which is defined to extend ObjectA while at the
> same time reusing the XmlElement name.
This is where your design doesn't seem very object-oriented. If ObjectB
extends ObjectA, do you intend that code should be able to refer to an
instance of ObjectB as though it were an ObjectA? In other words, are you
trying for polymorphism?
Or, are you just using implementation inheritance, in which case I need to
remind you that C# doesn't have private inheritance like C++ does. For
instance, in C++ you could do:
public class ObjectB : private ObjectA /* Please excuse any bad syntax. It's
been a while */
In the above case, no user of ObjectB could ever detect that it based some
of its implementation on ObjectA. But in C#, you have
public class ObjectB : ObjectA {}
In this case, there is no way to hide from a caller the fact that ObjectB is
based on ObjectA.
In particular, if you define Count in both classes, there really are two
separate and independant "Count" properties, and code can use them both:
ObjectB ob = new ObjectB();
int cb = ob.Count;
int ca = ((ObjectA) ob).Count;
If "Count" were virtual, then the two assignments above would be equivalent.
John
Techno_Dex - 18 Oct 2006 18:03 GMT
Possibly both need to be available. As for the intention of the example I
have given, I'm looking for Inheritence and hiding. The XSD is redefining
the ObjectA to extend it, which results in ObjectB.
Both of these need to be available
BaseClass oBC = new BaseClass();
oBC.Count.FavoritNum = 43;
which generate XML Serialized output of
<Foo>
<Count FavNum=43 />
</Foo>
SubClass oSC = new SubClass();
oSC.Count.FavoriteNum = 60;
oSC.Count.FavoriteLetter = 1;
which generate XML Serialized output of
<Bar>
<Count FavNum=60 FavLetter=1 />
</Bar>
>> Bad example on my part as I used a simple type consisting of ints. I was
>> trying to keep is as simple as possible but that isn't going to work in
[quoted text clipped - 37 lines]
>
> John
John Saunders - 18 Oct 2006 23:37 GMT
> Possibly both need to be available. As for the intention of the example I
> have given, I'm looking for Inheritence and hiding. The XSD is redefining
[quoted text clipped - 17 lines]
> <Count FavNum=60 FavLetter=1 />
> </Bar>
What happened to BaseClass.Count?
Perhaps you could post your schema? I bet you're using derivation by
extension in a way that doesn't map to OOP.
John
Techno_Dex - 19 Oct 2006 17:01 GMT
BaseClass.Count (which returns something of type ObjectA) was redefined to
be SubClass.Count (which returns something of type ObjectB which inherits
from and extends ObjectA)
The schema looks something like the following
<xsd:schema xmlns:xsd="">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Foo">
<xsd:attribute name="Count" type="int" />
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
<xsd:schema xmlns:xsd="">
<xsd:redefine schemalocation="BaseClass.xsd">
<xsd:complexType name="Bar">
<xsd:complexContext>
<xsd:extension base="Foo">
<xsd:attribute name="FavLetter" type="int" />
</xsd:extension>
</xsd:complexContext>
</xsd:complexType>
</xsd:redefine>
</xsd:schema>
>> Possibly both need to be available. As for the intention of the example
>> I have given, I'm looking for Inheritence and hiding. The XSD is
[quoted text clipped - 24 lines]
>
> John
John Saunders - 20 Oct 2006 12:15 GMT
> BaseClass.Count (which returns something of type ObjectA) was redefined to
> be SubClass.Count (which returns something of type ObjectB which inherits
[quoted text clipped - 23 lines]
> </xsd:redefine>
> </xsd:schema>
Yeah, like I said, this doesn't map to a standard OO paradigm in a
strongly-typed language. There are many parts of XSD which don't map that
way.
C#, for instance, doesn't support covariant return types (at least not in
1.1), which is kind of what you are describing in your text.
OTOH, although I don't use xsd:redefine, is that really what you're defining
above? Or, are you adding the FavLetter attribute and not removing Count?
John
Techno_Dex - 25 Oct 2006 16:19 GMT
It could be both. I hoping to develop some possible inheritance using
classes so I wouldn't have to rewrite all the base code in every xsd
extension/redefinition. In .NET 2.0 there appears to possibly be an XML
class which lets you override/replace existing elements and attributes in
the schema hierarchy but that looks to be painful. I guess I will just have
to cut and paste code. It's cleaner than writing a 5000 line method of
XmlWriteElement statements.
>> BaseClass.Count (which returns something of type ObjectA) was redefined
>> to be SubClass.Count (which returns something of type ObjectB which
[quoted text clipped - 36 lines]
>
> John