在运行时动态转换 [英] Dynamic casting at runtime

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

问题描述




这是我提出的解决方案带来的问题

我有另一个问题,那就是动态对象创作" ;.所以,如果你能动态投射一个物体,我很好奇。如果你有两个具有公共基类的对象

,它们都可以被强制转换为基类

类,但是如果其中一个子类具有非常规方法你将

无法访问它们。现在我知道后期绑定和所有那些

的东西,但我需要一个更轻量级的解决方案。


所以我希望有一个实例基类,并根据在运行时决定的内容将它们转换为子节点之间的
。但是

我不知道怎么代表这个类,所以它可以被强制转换,并且很好。


我最初认为有System.Type变量代表两个

子类,然后是第三个System.Type,我选择了〜>
的子类型,第三个可能是

" cast to"类变量,但我不知道如何代表班级

所以演员会工作。


所以这样,


System.Type childA = typeof(childA);

System.Type childB = typeof(childB);


System.Type generalChild ="用户想要的孩子;


父母= null;


//程序中的后期

((generalChild)父母)。孩子的正确方法

generalChild


但我无法想象如何以某种方式表示一个类,形成一个类型,系统将允许这个编译的
。事先得到帮助。

Hi,

This is a question brought about by a solution I came up with to
another question I had, which was "Dynamic object creation". So, I''m
curious if you can dynamically cast an object. If you have two object
which have a common base class, they can both be cast up to the base
class, but if either of the child classes have unuque methods you will
not be able to access them. Now I know about late binding and all that
stuff, but I need a much more light weight solution.

So I was hoping to have an instance of the base class, and cast it
between the children depending on what''s decided during runtime. But
I''m not sure how to represent the class so it can be cast, and chaged.

I originally thought to have System.Type variables representing the two
child classes, and then a third System.Type which I would = to which
ever of the child types was chosen, and that third one could be the
"cast to" class variable, but I don''t know how to represent the class
so the cast will work.

So like this,

System.Type childA = typeof(childA);
System.Type childB = typeof(childB);

System.Type generalChild = "which ever child the user wantes";

Parent parent = null;

//later in the program
((generalChild)parent)."the correct method for the child in
generalChild"

But I can''t figure out how to represent a class in a way, form a type,
that the system will let this compile. THanks in advance for the help.

推荐答案

没有办法做到这一点。反映是这个

案例的唯一选择。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


DaTurk < mm ****** @ hotmail.comwrote in message

news:11 ********************** @ p79g2000cwp .googlegr oups.com ...
There is no way to do this. Reflection is your only option in this
case.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...




这是我提出的解决方案带来的问题与

我有另一个问题,即动态对象创建。所以,如果你能动态投射一个物体,我很好奇。如果你有两个具有公共基类的对象

,它们都可以被强制转换为基类

类,但是如果其中一个子类具有非常规方法你将

无法访问它们。现在我知道后期绑定和所有那些

的东西,但我需要一个更轻量级的解决方案。


所以我希望有一个实例基类,并根据在运行时决定的内容将它们转换为子节点之间的
。但是

我不知道怎么代表这个类,所以它可以被强制转换,并且很好。


我最初认为有System.Type变量代表两个

子类,然后是第三个System.Type,我选择了〜>
的子类型,第三个可能是

" cast to"类变量,但我不知道如何代表班级

所以演员会工作。


所以这样,


System.Type childA = typeof(childA);

System.Type childB = typeof(childB);


System.Type generalChild ="用户想要的孩子;


父母= null;


//程序中的后期

((generalChild)父母)。孩子的正确方法

generalChild


但我无法想象如何以某种方式表示一个类,形成一个类型,系统将允许这个编译的
。提前帮忙谢谢。
Hi,

This is a question brought about by a solution I came up with to
another question I had, which was "Dynamic object creation". So, I''m
curious if you can dynamically cast an object. If you have two object
which have a common base class, they can both be cast up to the base
class, but if either of the child classes have unuque methods you will
not be able to access them. Now I know about late binding and all that
stuff, but I need a much more light weight solution.

So I was hoping to have an instance of the base class, and cast it
between the children depending on what''s decided during runtime. But
I''m not sure how to represent the class so it can be cast, and chaged.

I originally thought to have System.Type variables representing the two
child classes, and then a third System.Type which I would = to which
ever of the child types was chosen, and that third one could be the
"cast to" class variable, but I don''t know how to represent the class
so the cast will work.

So like this,

System.Type childA = typeof(childA);
System.Type childB = typeof(childB);

System.Type generalChild = "which ever child the user wantes";

Parent parent = null;

//later in the program
((generalChild)parent)."the correct method for the child in
generalChild"

But I can''t figure out how to represent a class in a way, form a type,
that the system will let this compile. THanks in advance for the help.



你能不能给我一个怎么做的摘要?所有这些例子我已经找到了很长时间的啰嗦,而且根本没有实际的价值。我不妨创建一个单独的类。


Nicholas Paldino [.NET / C#MVP]写道:
Would it be possible for you to give me an exaple of how to do it? All
fo the examples I''ve found a very long winded, and not at all
practical. I might as well just create a seperate class.

Nicholas Paldino [.NET/C# MVP] wrote:

那里没办法做到这一点。反映是这个

案例的唯一选择。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard。 caspershouse.com


" DaTurk" < mm ****** @ hotmail.comwrote in message

news:11 ********************** @ p79g2000cwp .googlegr oups.com ...
There is no way to do this. Reflection is your only option in this
case.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...




这是我提出的解决方案带来的问题与

我有另一个问题,即动态对象创建。所以,如果你能动态投射一个物体,我很好奇。如果你有两个具有公共基类的对象

,它们都可以被强制转换为基类

类,但是如果其中一个子类具有非常规方法你将

无法访问它们。现在我知道后期绑定和所有那些

的东西,但我需要一个更轻量级的解决方案。


所以我希望有一个实例基类,并根据在运行时决定的内容将它们转换为子节点之间的
。但是

我不知道怎么代表这个类,所以它可以被强制转换,并且很好。


我最初认为有System.Type变量代表两个

子类,然后是第三个System.Type,我选择了〜>
的子类型,第三个可能是

" cast to"类变量,但我不知道如何代表班级

所以演员会工作。


所以这样,


System.Type childA = typeof(childA);

System.Type childB = typeof(childB);


System.Type generalChild ="用户想要的孩子;


父母= null;


//程序中的后期

((generalChild)父母)。孩子的正确方法

generalChild


但我无法想象如何以某种方式表示一个类,形成一个类型,系统将允许这个编译的
。在此先感谢您的帮助。
Hi,

This is a question brought about by a solution I came up with to
another question I had, which was "Dynamic object creation". So, I''m
curious if you can dynamically cast an object. If you have two object
which have a common base class, they can both be cast up to the base
class, but if either of the child classes have unuque methods you will
not be able to access them. Now I know about late binding and all that
stuff, but I need a much more light weight solution.

So I was hoping to have an instance of the base class, and cast it
between the children depending on what''s decided during runtime. But
I''m not sure how to represent the class so it can be cast, and chaged.

I originally thought to have System.Type variables representing the two
child classes, and then a third System.Type which I would = to which
ever of the child types was chosen, and that third one could be the
"cast to" class variable, but I don''t know how to represent the class
so the cast will work.

So like this,

System.Type childA = typeof(childA);
System.Type childB = typeof(childB);

System.Type generalChild = "which ever child the user wantes";

Parent parent = null;

//later in the program
((generalChild)parent)."the correct method for the child in
generalChild"

But I can''t figure out how to represent a class in a way, form a type,
that the system will let this compile. THanks in advance for the help.


DaTurk,

我相信你最好的方法是调查你的对象

都实现了相同的接口。然后你可以创建一个

IMyObject的实例,无论实际的类是什么,并可靠地调用它的基于接口的

方法。

Peter


-

联合创始人,Eggheadcafe.com开发者门户网站:
http://www.eggheadcafe.com

UnBlog:
http://petesbloggerama.blogspot.com


" DaTurk"写道:
DaTurk,
I believe your best approach here would be to look into having your objects
all implement the same interface. You can then create an instance of
IMyObject no matter what the actual class is, and call its interface - based
methods reliably.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"DaTurk" wrote:




这是我提出的解决方案带来的问题

我有另一个问题,即动态对象创建。所以,如果你能动态投射一个物体,我很好奇。如果你有两个具有公共基类的对象

,它们都可以被强制转换为基类

类,但是如果其中一个子类具有非常规方法你将

无法访问它们。现在我知道后期绑定和所有那些

的东西,但我需要一个更轻量级的解决方案。


所以我希望有一个实例基类,并根据在运行时决定的内容将它们转换为子节点之间的
。但是

我不知道怎么代表这个类,所以它可以被强制转换,并且很好。


我最初认为有System.Type变量代表两个

子类,然后是第三个System.Type,我选择了〜>
的子类型,第三个可能是

" cast to"类变量,但我不知道如何代表班级

所以演员会工作。


所以这样,


System.Type childA = typeof(childA);

System.Type childB = typeof(childB);


System.Type generalChild ="用户想要的孩子;


父母= null;


//程序中的后期

((generalChild)父母)。孩子的正确方法

generalChild


但我无法想象如何以某种方式表示一个类,形成一个类型,系统将允许这个编译的
。事先得到帮助。

Hi,

This is a question brought about by a solution I came up with to
another question I had, which was "Dynamic object creation". So, I''m
curious if you can dynamically cast an object. If you have two object
which have a common base class, they can both be cast up to the base
class, but if either of the child classes have unuque methods you will
not be able to access them. Now I know about late binding and all that
stuff, but I need a much more light weight solution.

So I was hoping to have an instance of the base class, and cast it
between the children depending on what''s decided during runtime. But
I''m not sure how to represent the class so it can be cast, and chaged.

I originally thought to have System.Type variables representing the two
child classes, and then a third System.Type which I would = to which
ever of the child types was chosen, and that third one could be the
"cast to" class variable, but I don''t know how to represent the class
so the cast will work.

So like this,

System.Type childA = typeof(childA);
System.Type childB = typeof(childB);

System.Type generalChild = "which ever child the user wantes";

Parent parent = null;

//later in the program
((generalChild)parent)."the correct method for the child in
generalChild"

But I can''t figure out how to represent a class in a way, form a type,
that the system will let this compile. THanks in advance for the help.


这篇关于在运行时动态转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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