转换为派生类 [英] casting to a derived class

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

问题描述

有人可以在运行时告诉我正确的投射方法和对象。

以下是我正在尝试做的一个示例。 classA充当类的基础

,它们在从ProBase派生的类中执行代码。如何在运行时将属性从ProBase转换为
,以便我可以访问他们的

唯一方法?


class抽象类A

{

公共类A(ProBase pb)

{

this.Execute(pb); < br $>
}

public abstract void执行(ProBase bp);

}


class classB:classA

{

公共classB(ProBase bp):classA(bp){}

public override void Execute(ProBase bp)

{

System.Type type = bp.GetType();

((type)bp).DoIt();

}

}

class classC:classA

{

public classC(ProBase bp):classA(bp) ){}

public override void Execute(ProBase bp)

{

System.Type type = bp.GetType();

((type)bp).DoThis();

}

}

class ProBase

{

公共ProBase(){}

}


clas s ProBaseA:ProBase

{

public ProBaseA():ProBase(){}

public DoIt()

{

....做点什么;

}

}


类ProBaseB: ProBase

{

public ProBaseB():ProBase(){}

public DoThis()

{

....做点什么;

}

}


main()

{

classB b = new classB(new ProBaseA());

classC c = new classC(new ProBaseB());

}


-

史蒂夫

Can someone tell me the correct method of casting and object at run time.
Here is a sample of what I''m trying to do. classA acts as a base for classes
that execute code in classes derived from ProBase. How do I property cast
the derived classes from ProBase during runtime so that I can access their
unique methods?

class abstract classA
{
public classA(ProBase pb)
{
this.Execute(pb);
}
public abstract void Execute(ProBase bp);
}

class classB : classA
{
public classB(ProBase bp) : classA(bp) { }
public override void Execute(ProBase bp)
{
System.Type type = bp.GetType();
((type)bp).DoIt();
}
}
class classC : classA
{
public classC(ProBase bp) : classA(bp) { }
public override void Execute(ProBase bp)
{
System.Type type = bp.GetType();
((type)bp).DoThis();
}
}
class ProBase
{
public ProBase() { }
}

class ProBaseA : ProBase
{
public ProBaseA() : ProBase() {}
public DoIt()
{
.... do something;
}
}

class ProBaseB : ProBase
{
public ProBaseB() : ProBase() {}
public DoThis()
{
.... do something;
}
}

main()
{
classB b = new classB(new ProBaseA());
classC c = new classC(new ProBaseB());
}

--
Steve

推荐答案

史蒂夫Teeples< St ********** @ discussion.microsoft.com>写道:
Steve Teeples <St**********@discussions.microsoft.com> wrote:
有人可以在运行时告诉我正确的投射方法和对象。
以下是我正在尝试做的一个示例。 classA充当类的基础,用于在从ProBase派生的类中执行代码。如何在运行期间从ProBase转换派生类,以便我可以访问他们的
独特方法?
Can someone tell me the correct method of casting and object at run time.
Here is a sample of what I''m trying to do. classA acts as a base for classes
that execute code in classes derived from ProBase. How do I property cast
the derived classes from ProBase during runtime so that I can access their
unique methods?




如果你知道方法在编译时,你必须知道涉及哪个类是
,所以只需要转换为该类。在B课你会这样做:


((ProBaseA)bp)。DoIt();


你可以这样做:


((ProBaseB)bp).DoThis();


首先考虑一下需要这样做的方法 -

通常有更好的工作方式。


-

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

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



If you know the methods at compile time, you must know which class is
involved, so just cast to that class. In classB you would do:

((ProBaseA)bp).DoIt();

and in classC you would do:

((ProBaseB)bp).DoThis();

Have a think about a way round needing to do this in the first place -
there are usually better ways of working than that.

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


Jon,


ProBase有超过10个派生类,因此任何这些类

都可以在运行时以任何顺序传递到这个单例程中。

有没有办法做到这一点?如果没有,你知道更好的方法吗?我总是在游戏中学习更好的方法。


" Jon Skeet [C#MVP]"写道:
Jon,

There are more than 10 derived classes for ProBase so any of those classes
could be passed into this single routine during run time in any order. Is
there no way of doing this? If not, do you know of a better method? I''m
always game to learn better approaches.

"Jon Skeet [C# MVP]" wrote:
Steve Teeples< St ********** @ discussion.microsoft.com>写道:
Steve Teeples <St**********@discussions.microsoft.com> wrote:
有人可以在运行时告诉我正确的投射方法和对象。
以下是我正在尝试做的一个示例。 classA充当类的基础,用于在从ProBase派生的类中执行代码。如何在运行时期间从ProBase转换派生类,以便我可以访问他们独特的方法?
Can someone tell me the correct method of casting and object at run time.
Here is a sample of what I''m trying to do. classA acts as a base for classes
that execute code in classes derived from ProBase. How do I property cast
the derived classes from ProBase during runtime so that I can access their
unique methods?



如果您在编译时知道这些方法,你必须知道涉及哪个班级,所以只需要投入到那个班级。在B班你会这样做:

((ProBaseA)bp)。DoIt();

在课堂上你会这样做:

(( ProBaseB)bp).DoThis();

首先考虑一下需要这样做的方法 -
通常有更好的工作方式。

-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



If you know the methods at compile time, you must know which class is
involved, so just cast to that class. In classB you would do:

((ProBaseA)bp).DoIt();

and in classC you would do:

((ProBaseB)bp).DoThis();

Have a think about a way round needing to do this in the first place -
there are usually better ways of working than that.

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



Steve Teeples< St ********** @ discussion.microsoft.com>写道:
Steve Teeples <St**********@discussions.microsoft.com> wrote:
ProBase有超过10个派生类,因此任何这些类都可以在运行时以任何顺序传递到这个单例程中。
有没有办法做到这一点?如果没有,你知道更好的方法吗?我总是在游戏中学习更好的方法。
There are more than 10 derived classes for ProBase so any of those classes
could be passed into this single routine during run time in any order. Is
there no way of doing this? If not, do you know of a better method? I''m
always game to learn better approaches.




再次,如果你知道需要调用哪种方法,你必须知道

在编译时键入,所以只需转换为该类型。


这些方法真的没有共同的目的吗?通常你会在基类中放一个

抽象方法,但是如果不知道真正的

情况,就很难说最好的方法。


-

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

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



Again, if you know which method you need to call, you must know the
type at compile time, so just cast to that type.

Do the methods really have no common purpose? Usually you''d put an
abstract method in the base class, but without knowing the real
situation, it''s hard to say the best way to proceed.

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


这篇关于转换为派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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