473,396 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How: To get true dynamic polymorphism (i.e. smalltalk)

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().D ocumentElement));
response.Append("\r\n");
}
}
return response.ToString();
}
Nov 15 '05 #1
10 5727
To put this in TCL/TK or Smalltalk way of speaking I am looking for the
equivelent of

(random object).performMethod("named method")

I.e. I want to invoke methods dynamically at run-time

Assume I already did the checking that this method is implemented by the
object, so that I don't get a run-time error

[Yes, I am not a fan of 100% rigidly hard-typed systems, since there are
times where one needs to get around this]
--
==================================
Yechezkal Gutfreund
Chief Scientist
Kesser Technical Group, Inc.
==================================
"Yechezkal Gutfreund" <sg********@hotmail.com> wrote in message
news:eJ**************@TK2MSFTNGP10.phx.gbl...
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().D ocumentElement));
response.Append("\r\n");
}
}
return response.ToString();
}

Nov 15 '05 #2
Yechezkal Gutfreund wrote:
I.e. I want to invoke methods dynamically at run-time


Then all you need do is mark the ToXml method as virtual in the
SpriteModel class. In each of the drived classes, mark the ToXml method
as an override. Just as in C++ and many other languages, this will cause
the most derived implementation of the method to execute when invoked
from a reference to a base class.

Once done, you can cast every object as a SpriteModel and you'll get the
correct results.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #3
Then go code SML or some other weird language.
"Yechezkal Gutfreund" <sg********@hotmail.com> wrote in message
news:OC**************@TK2MSFTNGP11.phx.gbl...
To put this in TCL/TK or Smalltalk way of speaking I am looking for the
equivelent of

(random object).performMethod("named method")

I.e. I want to invoke methods dynamically at run-time

Assume I already did the checking that this method is implemented by the
object, so that I don't get a run-time error

[Yes, I am not a fan of 100% rigidly hard-typed systems, since there are
times where one needs to get around this]
--
==================================
Yechezkal Gutfreund
Chief Scientist
Kesser Technical Group, Inc.
==================================
"Yechezkal Gutfreund" <sg********@hotmail.com> wrote in message
news:eJ**************@TK2MSFTNGP10.phx.gbl...
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().D ocumentElement));
response.Append("\r\n");
}
}
return response.ToString();
}


Nov 15 '05 #4
You could use reflection to navigate the class hierarchie of your object and
choose the implementation of the method XYZ you are looking for. (look into
System.Reflection namespace)

JPRoot

"Yechezkal Gutfreund" <sg********@hotmail.com> wrote in message
news:OC**************@TK2MSFTNGP11.phx.gbl...
To put this in TCL/TK or Smalltalk way of speaking I am looking for the
equivelent of

(random object).performMethod("named method")

I.e. I want to invoke methods dynamically at run-time

Assume I already did the checking that this method is implemented by the
object, so that I don't get a run-time error

[Yes, I am not a fan of 100% rigidly hard-typed systems, since there are
times where one needs to get around this]
--
==================================
Yechezkal Gutfreund
Chief Scientist
Kesser Technical Group, Inc.
==================================
"Yechezkal Gutfreund" <sg********@hotmail.com> wrote in message
news:eJ**************@TK2MSFTNGP10.phx.gbl...
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().D ocumentElement));
response.Append("\r\n");
}
}
return response.ToString();
}


Nov 15 '05 #5

Hi Yechezkal,

I found that this post has been posted in several groups.
I have replied you in microsoft.public.dotnet.framework group. Please
follow up there, I will work with you.
Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #6
Yechezkal Gutfreund wrote:
I have two subclasses of SpriteModel (1) LocalSprite (2)Sprite

Both implement a method called .ToXml() which returns an XmlDocument. But
they are different.


Being a *really* long time Smalltalker (among other languages) I can tell
you: dont try to emulate the inheritance/method lookup/polymorphism of an
untyped language in a type language. Class trees grow differently in C#
compared to Smalltalk (and I prefer the way of Smalltalk of real life
modelling).

You have to understand that typed class hierarchies model implementation
variance while a Smalltalk class hierarchy models a behaviour variance.

To achive a Smalltalk-alike model you need to group classes of similar
behavior (behavior equivalence classes) in your implementation class trees
through interfaces. That means, all classes of a Smalltalk model that can be
used in Smalltalk at the same place because they have the same behavior now
need to be put into an interface. Since you cannot predict who is using your
class hierarchy using what polymorphy, you either implement as many
interfaces as sensible or you distribute source.

In you trivial example, introduce an interface encapsuling ToXml() and cast
your subclass instances to that interface. Then the right method is invoked
(irregardless of new, virtual and override modifiers).

Summary: translate a Smalltalk model class hierarchy by putting the class
behavior into interfaces and let possibly unrelated C# classes implement
these interfaces. Then to use instances in a polymorphic way, cast to the
interfaces.

Hope that helped a bit.

Andreas
--
Dipl.Inform. Andreas Tönne * Prokurist
Georg Heeg eK * Informatikleitung
mailto:at*****@heeg.de * http://www.heeg.de
phone x49 231 9 75 99 0/36 * fax x49 231 9 75 99 20
-------------------------------------------------------------------
Privat: mailto:at*****@t-online.de * http://www.atoenne.de
Nov 15 '05 #7
Frank,

Are you really sure this works?

As you can see from my code, the object of type Sprite and SpriteLocal are
stored in a Hashtable.
When you pull then out (e.g. mySprite = theHashTable[key]) you are given
something of type
object. You will get a syntax error unless you say:

Object mySprite = theHashTable[key]

or

SpriteMode mySprite = (SpriteModel)theHashTable[key]

This is gotcha of strong type-ing.

I have to forcibly cast this object to either an Object, Sprite,
SpriteModel, or SpriteLocal. But since there
are mixed items in this table, I don't know the answer till runtime.

Currenly, I am casting everything to (SpriteModel) but then it never invokes
the child's ToXML() method. but rather the SpriteModel version.

Reflection, on the other hand, should allow runtime invokation.
"Frank Oquendo" <fr*******@acadx.com> wrote in message
news:uB*************@TK2MSFTNGP10.phx.gbl...
Yechezkal Gutfreund wrote:
I.e. I want to invoke methods dynamically at run-time


Then all you need do is mark the ToXml method as virtual in the
SpriteModel class. In each of the drived classes, mark the ToXml method
as an override. Just as in C++ and many other languages, this will cause
the most derived implementation of the method to execute when invoked
from a reference to a base class.

Once done, you can cast every object as a SpriteModel and you'll get the
correct results.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)

Nov 15 '05 #8
Andreas Tönne wrote:
In you trivial example, introduce an interface encapsuling ToXml()
and cast your subclass instances to that interface. Then the right
method is invoked (irregardless of new, virtual and override
modifiers).


While interfaces are quite useful, they should be used as intended: for
homogeneous handling of heterogeneous objects. Such is not the case
here. A little attention to detail, not additional layers of
indirection, is all that is needed to solve the problem.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #9
Yechezkal,
This is gotcha of strong type-ing. If you want to avoid the benefits of strong type-ing, then you may want to
stay with Small Talk or try your hand at VB.NET. VB.NET is not as "loose-ly"
typed as Small Talk, however when you have Option Strict Off in VB.NET you
have much the same benefits as small talk, as you incur the expense of a
runtime check to see if the method is implemented on that object.

In other words VB.NET hides the reflection lookup from you, in much the same
method as Small Talk does, and allows you to call any method on a object you
want. At runtime it will either succeed or fail, depending on whether the
object actually supports that method.

Personally I find the performance & safety of strong type-ing far outways
the perceived benefits of late binding. However the late binding (Option
Strict Off) in VB.Net is very helpful in a number of cases...

Hope this helps
Jay
"Yechezkal Gutfreund" <sg********@hotmail.com> wrote in message
news:eD**************@tk2msftngp13.phx.gbl... Frank,

Are you really sure this works?

As you can see from my code, the object of type Sprite and SpriteLocal are
stored in a Hashtable.
When you pull then out (e.g. mySprite = theHashTable[key]) you are given
something of type
object. You will get a syntax error unless you say:

Object mySprite = theHashTable[key]

or

SpriteMode mySprite = (SpriteModel)theHashTable[key]

This is gotcha of strong type-ing.

I have to forcibly cast this object to either an Object, Sprite,
SpriteModel, or SpriteLocal. But since there
are mixed items in this table, I don't know the answer till runtime.

Currenly, I am casting everything to (SpriteModel) but then it never invokes the child's ToXML() method. but rather the SpriteModel version.

Reflection, on the other hand, should allow runtime invokation.

Nov 15 '05 #10
[This followup was posted to microsoft.public.dotnet.framework and a
copy was sent to the cited author.]

In article <OC**************@TK2MSFTNGP11.phx.gbl>,
sg********@hotmail.com says...
To put this in TCL/TK or Smalltalk way of speaking I am looking for the
equivelent of

(random object).performMethod("named method")

I.e. I want to invoke methods dynamically at run-time

Assume I already did the checking that this method is implemented by the
object, so that I don't get a run-time error

[Yes, I am not a fan of 100% rigidly hard-typed systems, since there are
times where one needs to get around this]

In my opinion Frank Oquendo's response is in the correct track.
Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sebastian Faust | last post by:
Hi, I am thinking now for a while about a design decision. I would be glad if you can give me some advices which is maybe the better way for solving my problem. I wanna do something like...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
2
by: Denon | last post by:
My system has some network interconnecting data, like node 1 <-> node 2 How to dynamic build a circuit diagram in from of (pdf,visio, or other graphical format)by ASP.NET? Thanks for your...
15
by: rwf_20 | last post by:
I just wanted to throw this up here in case anyone smarter than me has a suggestion/workaround: Problem: I have a classic producer/consumer system which accepts 'commands' from a socket and...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
1
by: Risen | last post by:
Hi, How to Dynamic adjust Button's Font property? Thx.
1
by: =?Utf-8?B?Sm9l?= | last post by:
how to dynamic assign two dimensions array size say Dim myArray(x, y) as Integer
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.