虚方法的问题 [英] Problem with virtual method

查看:42
本文介绍了虚方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个带有虚方法的类,并且子类覆盖了虚拟方法,那么我创建子类的实例AS A

基类...

BaseClass bc = new ChildClass();


....然后调用虚方法,为什么是否调用了基类的方法

而不是被重写的方法?如果我不知道
在运行时知道子类是什么的,我该如何解决这个问题呢?我正在使用

Activator.CreateInstance()从附属DLL加载子类,这是继承我的基类的第二方插件,所以因此我无法在代码中或编译时声明子类类型。


谢谢,

Jon

解决方案

但是您是否尝试使用

Activator.CreateInstance()进行实例化的卫星DLL?

否则我们只是证明了重写的虚拟

方法和新方法之间的区别。关键字,覆盖的工作。


老实说,但我的另一个DLL中的子类覆盖了我的

虚方法和基本虚方法被称为。我正在通过代码踩到
,基类出现在

方法的代码窗口中。它绝对标记为虚拟。孩子班绝对是一个孩子(公共班MyChild:MyBase),而且这个方法肯定有

"覆盖"关键字。


这让我难倒了至少一个星期,而且我不能再在这个项目上输掉
!我必须继续前进!但是这个插件功能是这个产品的核心亮点之一。


到底是怎么回事? :( :(


Jon

" Alien"< al *** @ sympatico.ca>写在留言中

新闻:ug **************** @ news02.bloor.is.net.cable.rogers.com ...

那是奇怪的我已经复制了(我认为)你在做什么,在我的情况下,我会调用覆盖的
方法。这就是我所拥有的:

public class Test {
public virtual void testVoid(){
Console.WriteLine(" base void!);
}

公共类TestChild:测试{
公共覆盖void testVoid(){
Console.WriteLine(" child void!);
}
}

上课,我有:

测试t = new TestChild();
t.testVoid();

我得到的输出是:
孩子无效!

Jon Davis"< jo*@REMOVE.ME.PLEASE.jondavis.net>写信息
新闻:uF ********* ***** @ TK2MSFTNGP11.phx.gbl ... <块quote class =post_quotes>如果我有一个带有虚方法的类,并且
的子类重写了虚方法,那么我创建子类的实例AS
一个基类。 ..

BaseClass bc = new ChildClass();

...然后调用虚方法,为什么它是基类'的


方法

被调用而不是被重写的方法?如果我不在运行时知道子类是什么,我该如何解决这个问题呢?我正在使用
Activator.CreateInstance()从一个卫星DLL加载子类,
a继承我的基类的第三方插件,所以我因此无法声明代码中或编译时的子类类型。

谢谢,
Jon




Jon Davis< jo*@REMOVE.ME.PLEASE.jondavis.net>写道:

如果我有一个带有虚方法的类,并且子类覆盖了虚拟方法,那么我创建一个子类的实例AS A
基类...

BaseClass bc = new ChildClass();

...然后调用虚方法,为什么它是基类的方法
被调用而不是被覆盖的方法?如果我不在运行时知道子类是什么,我该如何解决这个问题呢?我正在使用
Activator.CreateInstance()从一个卫星DLL加载子类,一个继承我的基类的第三方插件,所以我因此无法声明代码中或编译时的子类类型。




我怀疑你没有正确覆盖该方法。你能发一个

简短而完整的例子来证明这个问题吗?请参阅
http://www.pobox.com/ ~siget / csharp / complete.html 我的意思。


-

Jon Skeet - < sk *** @ pobox .com>
http://www.pobox.com/~skeet/

如果回复小组,请不要给我发邮件


" Jon Skeet" < SK *** @ pobox.com>在留言新闻中写道:MP ************************ @ news.microsoft.com ......

我怀疑你没有正确地覆盖这个方法。你能发一个简短但完整的例子来证明这个问题吗?请参阅
http://www.pobox.com/ ~siget / csharp / complete.html 我的意思。




你已经说过这个并在之前的帖子中给出了这个链接。不,我不能因为我不知道如何在不显示所有代码的情况下重现问题。


这就是我所拥有的,类,实例,并且组中的对象名称已更改:


IN ASSEMBLY A:


//组名称已更改

公共抽象类MyType {


...


//这是调试人员在通过代码进行步骤时的情况

//名称,为组更改返回类型名称

公共虚拟MyResultType MyMethod(...){


..默认输出..


}


}


//组名称已更改

public class MyCallingType {


//名称,为组更改返回类型名称

public MyResultType [] MyActions(...){


...


//类型名称,组名称更改

MyTypeInstanceList myTypeInstances = Thing.MyLoadedExternalPlugIns;

...


for(int i = 0; I< myTypeInstances.Count; i ++){


//类型名称,名称已更改为组

MyType myTypeInstance = myTypeInstances [i];


//注意这一点调试员

//说这是一个MyNewType(见下文)


......


//为组改变了类型名称

MyResultType pr;


...


pr = myType.MyMethod(...);


...



}


}

}

组装B:


//组名称已更改

公共类MyNewType:MyType {


...


//这是调试人员应该去的地方

//在通过代码进行步骤时

//名称,为组更改返回类型名称

公共覆盖MyResultType MyMethod(...){


...


}


}


Jon


If I have a class with a virtual method, and a child class that overrides
the virtual method, and then I create an instance of the child class AS A
base class...

BaseClass bc = new ChildClass();

.... and then call the virtual method, why is it that the base class''s method
is called instead of the overridden method? How do I fix this if I don''t
know at runtime what the child class is? I''m using
Activator.CreateInstance() to load the child class from a satellite DLL, a
third-party plug-in that inherits my base class, so I therefore cannot
declare the child class type in the code or at compile time.

Thanks,
Jon

解决方案

But did you try it with a satellite DLL instantiated with
Activator.CreateInstance()?

Otherwise we''re just proving the difference between an overridden virtual
method and the "new" keyword, that overriding works.

Honest to God, though, my child class in an another DLL is overriding my
virtual method, and the base virtual method is being called. I''m stepping
through the code, and the base class comes up in the code window on that
method. It is definitely marked as "virtual". The child class is definitely
a child (public class MyChild : MyBase), and the method definitely has the
"override" keyword.

This has had me stumped for at least a week, and I CANNOT afford to lose
another day on this project!! I MUST move on! But this plug-in feature is
one of the core highlights of the product.

What the hell is going on?? :( :(

Jon
"Alien" <al***@sympatico.ca> wrote in message
news:ug****************@news02.bloor.is.net.cable. rogers.com...

That is odd
I''ve replicated what (I think) you''re doing, and in my case the overriden
method is called. This is what I have:

public class Test {
public virtual void testVoid() {
Console.WriteLine("base void!");
}
}

public class TestChild : Test {
public override void testVoid() {
Console.WriteLine("child void!");
}
}

In another class, I have:

Test t = new TestChild();
t.testVoid();

And the output I get is:
child void!

"Jon Davis" <jo*@REMOVE.ME.PLEASE.jondavis.net> wrote in message
news:uF**************@TK2MSFTNGP11.phx.gbl...

If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class...

BaseClass bc = new ChildClass();

... and then call the virtual method, why is it that the base class''s


method

is called instead of the overridden method? How do I fix this if I don''t
know at runtime what the child class is? I''m using
Activator.CreateInstance() to load the child class from a satellite DLL, a third-party plug-in that inherits my base class, so I therefore cannot
declare the child class type in the code or at compile time.

Thanks,
Jon




Jon Davis <jo*@REMOVE.ME.PLEASE.jondavis.net> wrote:

If I have a class with a virtual method, and a child class that overrides
the virtual method, and then I create an instance of the child class AS A
base class...

BaseClass bc = new ChildClass();

... and then call the virtual method, why is it that the base class''s method
is called instead of the overridden method? How do I fix this if I don''t
know at runtime what the child class is? I''m using
Activator.CreateInstance() to load the child class from a satellite DLL, a
third-party plug-in that inherits my base class, so I therefore cannot
declare the child class type in the code or at compile time.



I suspect you haven''t overridden the method properly. Could you post a
short but complete example which demonstrates the problem? See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


"Jon Skeet" <sk***@pobox.com> wrote in message news:MP************************@news.microsoft.com ...

I suspect you haven''t overridden the method properly. Could you post a
short but complete example which demonstrates the problem? See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean.



You already said this and gave that link in a prior thread. No I can''t because I don''t know how to reproduce the problem without displaying all of my code.

Here''s exactly what I have, with class, instance, and object names changed for the group:

IN ASSEMBLY A:

// name changed for group
public abstract class MyType {

...

// THIS IS WHERE THE DEBUGGER GOES WHEN STEPPING THROUGH THE CODE
// name, return type name changed for group
public virtual MyResultType MyMethod( ... ) {

.. default output ..

}

}

// name changed for group
public class MyCallingType {

// name, return type name changed for group
public MyResultType[] MyActions( ... ) {

...

// type names, names changed for group
MyTypeInstanceList myTypeInstances = Thing.MyLoadedExternalPlugIns;

...

for (int i=0; i<myTypeInstances.Count; i++) {

// type name, name changed for group
MyType myTypeInstance = myTypeInstances[i];

// NOTE THAT AT THIS POINT THE DEBUGGER
// SAYS THAT THIS IS A MyNewType (see below)

...

// type name changed for group
MyResultType pr;

...

pr = myType.MyMethod( ... );

...


}

}
}
IN ASSEMBLY B:

// names changed for group
public class MyNewType : MyType {

...

// THIS IS WHERE THE DEBUGGER SHOULD BE GOING
// WHEN STEPPING THROUGH THE CODE
// name, return type name changed for group
public override MyResultType MyMethod( ... ) {

...

}

}

Jon


这篇关于虚方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆