Y
Yechezkal Gutfreund
I have two subclasses of SpriteModel (1) LocalSprite (2)Sprite
Both implement a method called .ToXml() which returns an XmlDocument. But
they are different.
I instances of these objects stored in a Hashtable. Extracting them from the
Hashtable means that I get a object that is tentatively an instance of Class
Object (or Type Object to use the correct C# word).
I can HARD cast the object to a SpriteModel, and then invoke the .ToXml()
method, but if either of these sublcasses overrides or NEWs the default
method in SpriteModel. I can't get to it.
What I want to do is dynamically cast the object extracting from the
Hashtable to it's .GetType
realType = spriteList.Value.GetType()
and then do something like a dynamic cast (realType)spriteList.Value
Obviously, this is very wrong syntacically, but there most be something that
is kosher that does the same effect? right? I don't have to resort to
Reflection and P/Invoke do I?
public override string ToString()
{
StringBuilder response = new StringBuilder();
lock (this.spriteTable) {
IDictionaryEnumerator spriteList = this.spriteTable.GetEnumerator();
while (spriteList.MoveNext())
{
SpriteModel sprite = (SpriteModel)spriteList.Value;
response.Append(Tools.PrettyPrint(sprite.ToXml().DocumentElement));
response.Append("\r\n");
}
}
return response.ToString();
}
Both implement a method called .ToXml() which returns an XmlDocument. But
they are different.
I instances of these objects stored in a Hashtable. Extracting them from the
Hashtable means that I get a object that is tentatively an instance of Class
Object (or Type Object to use the correct C# word).
I can HARD cast the object to a SpriteModel, and then invoke the .ToXml()
method, but if either of these sublcasses overrides or NEWs the default
method in SpriteModel. I can't get to it.
What I want to do is dynamically cast the object extracting from the
Hashtable to it's .GetType
realType = spriteList.Value.GetType()
and then do something like a dynamic cast (realType)spriteList.Value
Obviously, this is very wrong syntacically, but there most be something that
is kosher that does the same effect? right? I don't have to resort to
Reflection and P/Invoke do I?
public override string ToString()
{
StringBuilder response = new StringBuilder();
lock (this.spriteTable) {
IDictionaryEnumerator spriteList = this.spriteTable.GetEnumerator();
while (spriteList.MoveNext())
{
SpriteModel sprite = (SpriteModel)spriteList.Value;
response.Append(Tools.PrettyPrint(sprite.ToXml().DocumentElement));
response.Append("\r\n");
}
}
return response.ToString();
}