473,802 Members | 2,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Valu e.GetType()

and then do something like a dynamic cast (realType)sprit eList.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.spriteTab le) {
IDictionaryEnum erator spriteList = this.spriteTabl e.GetEnumerator ();
while (spriteList.Mov eNext())
{
SpriteModel sprite = (SpriteModel)sp riteList.Value;
response.Append (Tools.PrettyPr int(sprite.ToXm l().DocumentEle ment));
response.Append ("\r\n");
}
}
return response.ToStri ng();
}
Nov 15 '05 #1
10 5766
To put this in TCL/TK or Smalltalk way of speaking I am looking for the
equivelent of

(random object).perform Method("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********@hot mail.com> wrote in message
news:eJ******** ******@TK2MSFTN GP10.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.Valu e.GetType()

and then do something like a dynamic cast (realType)sprit eList.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.spriteTab le) {
IDictionaryEnum erator spriteList = this.spriteTabl e.GetEnumerator ();
while (spriteList.Mov eNext())
{
SpriteModel sprite = (SpriteModel)sp riteList.Value;
response.Append (Tools.PrettyPr int(sprite.ToXm l().DocumentEle ment));
response.Append ("\r\n");
}
}
return response.ToStri ng();
}

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********@hot mail.com> wrote in message
news:OC******** ******@TK2MSFTN GP11.phx.gbl...
To put this in TCL/TK or Smalltalk way of speaking I am looking for the
equivelent of

(random object).perform Method("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********@hot mail.com> wrote in message
news:eJ******** ******@TK2MSFTN GP10.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.Valu e.GetType()

and then do something like a dynamic cast (realType)sprit eList.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.spriteTab le) {
IDictionaryEnum erator spriteList = this.spriteTabl e.GetEnumerator ();
while (spriteList.Mov eNext())
{
SpriteModel sprite = (SpriteModel)sp riteList.Value;
response.Append (Tools.PrettyPr int(sprite.ToXm l().DocumentEle ment));
response.Append ("\r\n");
}
}
return response.ToStri ng();
}


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.Reflecti on namespace)

JPRoot

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

(random object).perform Method("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********@hot mail.com> wrote in message
news:eJ******** ******@TK2MSFTN GP10.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.Valu e.GetType()

and then do something like a dynamic cast (realType)sprit eList.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.spriteTab le) {
IDictionaryEnum erator spriteList = this.spriteTabl e.GetEnumerator ();
while (spriteList.Mov eNext())
{
SpriteModel sprite = (SpriteModel)sp riteList.Value;
response.Append (Tools.PrettyPr int(sprite.ToXm l().DocumentEle ment));
response.Append ("\r\n");
}
}
return response.ToStri ng();
}


Nov 15 '05 #5

Hi Yechezkal,

I found that this post has been posted in several groups.
I have replied you in microsoft.publi c.dotnet.framew ork 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 * Informatikleitu ng
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)th eHashTable[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*******@acad x.com> wrote in message
news:uB******** *****@TK2MSFTNG P10.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********@hot mail.com> wrote in message
news:eD******** ******@tk2msftn gp13.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)th eHashTable[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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2214
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 described in the Bridge-Pattern. Therefore I have the following classes: Instrument InstrumentImpl InstrumentData
4
4433
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 reuse for various template parameters. I wonder if there exist techniques for achieving a dynamic polymorphism using the generic programming. Is this possible? If yes, can anyone show me simple examples in C++
2
2044
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 advice! Den
15
3045
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 'executes' them. Obviously, each different command (there are ~20 currently) has its own needed functionality. The dream goal here would be to remove all knowledge of the nature of the command at runtime. That is, I don't want ANY switch/cases...
13
14625
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 Nicolai M. Josuttis. Chapter 14.) How to implement analogue of this technique via static polymorphism? Perhaps there is special design pattern for this purpose...
1
1033
by: Risen | last post by:
Hi, How to Dynamic adjust Button's Font property? Thx.
1
1169
by: =?Utf-8?B?Sm9l?= | last post by:
how to dynamic assign two dimensions array size say Dim myArray(x, y) as Integer
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9561
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10281
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10058
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7597
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6835
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.