显然,这在语法上是非常错误的,但是大多数东西都是 犹太洁食会产生同样的效果吗?对?我不必诉诸于 反思和P / Invoke吗? 公共覆盖字符串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(" \\\\ nn;); } } 返回response.ToString(); } 解决方案 把它放在TCL / TK或Smalltalk说话的方式我正在寻找 等于 (随机对象) ).performMethod(命名方法) 即我想在运行时动态调用方法 假设我已经检查过这个方法是由 对象实现的,所以我不知道没有运行时错误 [是的,我不是100%严格硬类型系统的粉丝,因为有 次一个人需要绕过这个] - ========================= ========= Yechezkal Gutfreund 首席科学家 Kesser技术集团公司 ================================== " Yechezkal Gutfreund" < SG ******** @ hotmail.com>在消息中写道 新闻:eJ ************** @ TK2MSFTNGP10.phx.gbl ... 我有两个子类SpriteModel(1)LocalSprite(2)Sprite 两者都实现一个名为.ToXml()的方法,它返回一个XmlDocument。但是它们是不同的。 我将这些对象的实例存储在Hashtable中。从中提取它们Hashtable意味着我得到的对象暂时是类对象的实例(或类型对象使用正确的C#字)。 我可以很难将对象强制转换为SpriteModel,然后调用.ToXml()方法,但是如果其中任何一个子阶段覆盖或新建SpriteModel中的默认方法。我无法实现它。 我想要做的是动态地将对象提取从 Hashtable提取到它的.GetType realType = spriteList.Value.GetType() 然后执行像动态转换(realType)spriteList.Value 显然,这在语法上是非常错误的,但是大多数是犹太洁食的,会产生同样的效果吗?对?我不必诉诸于反射和P / Invoke吗? 公共覆盖字符串ToString() {String> StringBuilder响应=新的StringBuilder( ); lock(this.spriteTable){ IDictionaryEnumerator spriteList = this.spriteTable.GetEnumerator(); while(spriteList.MoveNext()) {/ / SpriteModel精灵=(SpriteModel)spriteList.Value; response.Append(Tools.PrettyPrint(sprite.ToXml()d ocumentElement)); response.Append(QUOT; \r\\\") ; } }返回response.ToString(); } Yechezkal Gutfreund写道: 即我想在运行时动态调用方法 然后你需要做的就是在 SpriteModel类中将ToXml方法标记为虚拟。在每个驱动类中,将ToXml方法标记为作为覆盖。就像在C ++和许多其他语言中一样,这将导致 从引用到基类时调用 时执行方法的最派生实现。 完成后,你可以将每个对象作为SpriteModel投射,你会得到 正确的结果。 - 有10种人。那些懂二元的人和那些没有b $ b的人。 http://code.acadx.com (拉针回复) 然后去代码SML或其他一些奇怪的语言。 " Yechezkal Gutfreund" < SG ******** @ hotmail.com>在消息中写道 新闻:OC ************** @ TK2MSFTNGP11.phx.gbl ... 把它放在TCL中/ TK或Smalltalk说话方式我正在寻找 (随机对象).performMethod(命名方法) 即我想在运行时动态调用方法 假设我已经检查过这个方法是由对象实现的,所以我没有得到运行时错误 [是的,我不是100%严格硬式系统的粉丝,因为有时间需要绕过这个] - - ================================== Yechezkal Gutfreund 酋长科学家 Kesser技术集团公司 ================================== br /> " Yechezkal Gutfreund" < SG ******** @ hotmail.com>在消息中写道新闻:eJ ************** @ TK2MSFTNGP10.phx.gbl ... 我有两个SpriteModel子类(1) LocalSprite(2)Sprite 两者都实现一个名为.ToXml()的方法,它返回一个XmlDocument。 但它们是不同的。 我将这些对象的实例存储在Hashtable中。从中提取它们 Hashtable意味着我得到一个暂时是的实例的对象 Class 对象(或Type对象使用正确的C#字)。 我可以将对象强制转换为SpriteModel,然后调用 ..ToXml()方法,但如果有的话这些sublcasses覆盖或NEWs SpriteModel中的默认方法。我无法实现它。 我想要做的是动态地将对象提取从 Hashtable提取到它的.GetType realType = spriteList.Value.GetType() 然后执行像动态转换(realType)spriteList.Value 显然,这在语法上是非常错误的,但是大多数是 是犹太洁食,会产生同样的效果吗?对?我不必诉诸于反射和P / Invoke吗? 公共覆盖字符串ToString() {String> StringBuilder响应=新的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(" \\\ n)" ; } }返回response.ToString(); } I have two subclasses of SpriteModel (1) LocalSprite (2)Sprite Both implement a method called .ToXml() which returns an XmlDocument. Butthey are different. I instances of these objects stored in a Hashtable. Extracting them from theHashtable means that I get a object that is tentatively an instance of ClassObject (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 defaultmethod in SpriteModel. I can''t get to it. What I want to do is dynamically cast the object extracting from theHashtable 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 thatis kosher that does the same effect? right? I don''t have to resort toReflection 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();} 解决方案 To put this in TCL/TK or Smalltalk way of speaking I am looking for theequivelent 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 theobject, so that I don''t get a run-time error [Yes, I am not a fan of 100% rigidly hard-typed systems, since there aretimes where one needs to get around this]--==================================Yechezkal GutfreundChief ScientistKesser Technical Group, Inc.=================================="Yechezkal Gutfreund" <sg********@hotmail.com> wrote in messagenews: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 fromthe Hashtable means that I get a object that is tentatively an instance ofClass 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 somethingthat 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(); }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 theSpriteModel class. In each of the drived classes, mark the ToXml methodas an override. Just as in C++ and many other languages, this will causethe most derived implementation of the method to execute when invokedfrom a reference to a base class. Once done, you can cast every object as a SpriteModel and you''ll get thecorrect results. --There are 10 kinds of people. Those who understand binary and those whodon''t. http://code.acadx.com(Pull the pin to reply)Then go code SML or some other weird language."Yechezkal Gutfreund" <sg********@hotmail.com> wrote in messagenews: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(); } 这篇关于方法:获得真正的动态多态性(即smalltalk)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 21:17