拆箱的替代方案.. [英] alternatives to unboxing..
问题描述
嗨!
我担心这个问题可能太基础了,但是作为一名c ++老手我不会在c#运行中找到一个好的设计。
//基类
类Vector {};
//继承类
类VectorChild: Vector {};
class VectorChildChild:VectorChild {};
//其他一些类
A级
{
public void vectorfun(Vector a,vector b){}
public override void fun(object a,object b){< br $>
// ***********************
//我丑陋的解决方案......
// ***********************
if(typeof(Vector)== a。 GetType())
vectorfun((Vector)a,(Vector)b); //取消装箱到矢量
if(typeof(VectorChild)== a.GetType())
vectorfun((VectorChild)a,(VectorChild)b); //取消装箱到
VectorChild
if(typeof(VectorChildChild)== a.GetType())
vectorfun((VectorChildChild)a, (VectorChildChild)b); //
拆箱至VectorChildChild
// ***********************
// ***********************
}
}
我想称之为有趣的使用Vector实例和VectorChild实例的A / $
实例的成员。
然而,我必须明确处理任何可能的类型使用
if(typeof()== GetType)
行...
即使明确定义VectorChild可以被转换成Vector ...
(从小就看起来很荒谬)我必须以丑陋的方式处理
对象,如上图所示。
做什么是正确的?
在C ++中,编译器能够自动抛出...我怎么能
实现这种行为
C#?
不幸的是我必须坚持使用对象类型,因为我的对象(
任意类型)存储在HashTable中。
我是c的新手,所以如果我错过了一些基本的
点的借口......
$ b你们希望告诉我的$ b ...
感谢您的评论
g?kha n
g?khan写道:< snip>
不幸的是我必须坚持使用对象类型,因为我的对象
(任意类型)存储在HashTable中。
仅供参考:C#2.0具有与STL类似的功能。搜索通用
会给你一些体面的点击。
有几件事:
1。您应该能够将''typeof(Vector)== a.GetType()''替换为''
是Vector'',如果你这样做,你必须检查对于子类之前
你检查父母
2.''(VectorChild)a''不是真正的拳击,它是一个演员。您也可能需要
来了解为什么你可以使用''作为VectorChild'而不是
我是一个完全新手的c#如果我错过一些基本的借口
点......
你们有希望告诉我......
感谢你的评论
g?khan
"克汗" <克******* @ gmail.com> écritdansle message de news:
11**********************@c74g2000cwc.googlegroups。 com ...
如果你正在处理参考类型,那么你不是拆箱,你只需要铸造它就是b $ b。
//基类
类Vector {};
//继承类
类VectorChild: Vector {};
class VectorChildChild:VectorChild {};
//其他一些类
A级
{
public void vectorfun(Vector a,vector b){}
public override void fun(object a,object b){< br $>
// ***********************
//我丑陋的解决方案......
// ***********************
if(typeof(Vector)== a。 GetType())
vectorfun((Vector)a,(Vector)b); //取消装箱到矢量
if(typeof(VectorChild)== a.GetType())
vectorfun((VectorChild)a,(VectorChild)b); //取消装箱到
VectorChild
if(typeof(VectorChildChild)== a.GetType())
vectorfun((VectorChildChild)a, (VectorChildChild)b); //
拆箱至VectorChildChild
// ***********************
// ***********************
}
}
我想称之为有趣的使用Vector实例和VectorChild实例的A / $
实例的成员。
那么为什么你有一个带来对象的方法乐趣,为什么不通过
强类型方法的向量/导数?
public void vectorfun(向量a,向量b){}
....将带任何Vector,VectorChild或VectorChildChild对象。
Joanna
-
Joanna Carter [TeamB]
顾问软件工程师
Joanna Carter [TeamB] schrieb:
" g?khan" <克******* @ gmail.com> écritdansle message de news:
11**********************@c74g2000cwc.googlegroups。 com ...
如果你正在处理参考类型,那么你不是拆箱,你只是在施展。
//基类
类Vector {};
//继承类
类VectorChild:Vector {};
类VectorChildChild:VectorChild {};
/ /其他一类
A类
{/>公共虚空vectorfun(矢量a,矢量b){}
公共覆盖void fun(对象a,对象b) {
// ***********************
//我丑陋的解决方案....
// * **********************
if(typeof(Vector)== a.GetType())
vectorfun((Vector)a ,(矢量)b); //取消装箱到Vector
if(typeof(VectorChild)== a.GetType())
vectorfun((VectorChild)a,(VectorChild)b); //取消装箱到
VectorChild
if(typeof(VectorChildChild)== a.GetType())
vectorfun((VectorChildChild)a,(VectorChildChild)b); //
取消装箱到VectorChildChild
// ***********************
// ****** *****************
}
}
我想称之为有趣。使用Vector实例和VectorChild实例的A实例的成员。
那么为什么你有一个带有对象的方法乐趣,为什么不将
Vector /衍生物传递给强类型方法?
公共void vectorfun(向量a,向量b){}
...将采取任何Vector,VectorChild或VectorChildChild对象。
< Joanna
- Joanna Carter [TeamB]
顾问软件工程师
A级{
public virtual void fun(object a,object b){}
}
class A0:A {
public override void fun(object a,object b){}
}
A.fun(a,b) - >问题!
因为我打电话给一些基类..这显然不是我的问题。
Hi!
I fear this question might be too basic, however being a c++ veteran I
have trouble to get a good desing in c# running.
// a base class
class Vector{};
// inherited class
class VectorChild:Vector{};
class VectorChildChild:VectorChild{};
// some other class
class A
{
public void vectorfun(Vector a,Vector b){}
public override void fun(object a,object b) {
// ***********************
// my ugly solution....
// ***********************
if (typeof(Vector) == a.GetType() )
vectorfun( (Vector) a , (Vector) b); // unboxing to Vector
if (typeof(VectorChild) == a.GetType() )
vectorfun( (VectorChild) a , (VectorChild) b); // unboxing to
VectorChild
if (typeof(VectorChildChild) == a.GetType() )
vectorfun( (VectorChildChild) a , (VectorChildChild) b); //
unboxing to VectorChildChild
// ***********************
// ***********************
}
}
I would like to call the "fun" member of an instance of A
using Vector instances and VectorChild instances.
However, sofar I have to explicitly deal with any possible type using
the
if (typeof () == GetType)
lines...
Even if explictly define that VectorChild can be casted into Vector...
( which seems to be ridicilous since its a child) I have to deal with
objects in the ugly way as shown above.
What is the right thing to do?
In C++ the compiler is able to automatically cast down... How can I
achieve this behaviour with
C#?
Unfortunately I have to stick with the object type since my objects (of
arbitrary type) are stored in a HashTable.
I am a totally newbie to c# so excuses if I miss some fundamental
point...
which you guys hopefully tell me...
Thanks for any comments
g?khan
g?khan wrote:<snip>
Unfortunately I have to stick with the object type since my objects
(of arbitrary type) are stored in a HashTable.
Just an FYI: C# 2.0 has functionality similar to STL. A search on generic
should give you some decent hits.
A couple of things:
1. You should be able to replace ''typeof(Vector) == a.GetType()'' with ''a
is Vector'', if you do that, you''ll have to check for the subclass before
you check for the parent
2. ''(VectorChild) a'' is not really boxing, its a cast. You also might want
to read up on why you might use ''a as VectorChild'' instead
I am a totally newbie to c# so excuses if I miss some fundamental
point...
which you guys hopefully tell me...
Thanks for any comments
g?khan
"g?khan" <g.*******@gmail.com> a écrit dans le message de news:
11**********************@c74g2000cwc.googlegroups. com...
If you are dealing with reference types, then you are not unboxing, you are
simply casting.
// a base class
class Vector{};
// inherited class
class VectorChild:Vector{};
class VectorChildChild:VectorChild{};
// some other class
class A
{
public void vectorfun(Vector a,Vector b){}
public override void fun(object a,object b) {
// ***********************
// my ugly solution....
// ***********************
if (typeof(Vector) == a.GetType() )
vectorfun( (Vector) a , (Vector) b); // unboxing to Vector
if (typeof(VectorChild) == a.GetType() )
vectorfun( (VectorChild) a , (VectorChild) b); // unboxing to
VectorChild
if (typeof(VectorChildChild) == a.GetType() )
vectorfun( (VectorChildChild) a , (VectorChildChild) b); //
unboxing to VectorChildChild
// ***********************
// ***********************
}
}
I would like to call the "fun" member of an instance of A
using Vector instances and VectorChild instances.
Then why have you got a method fun that takes objects, why not pass the
Vector/derivatives to the strongly typed method ?
public void vectorfun(Vector a,Vector b){}
....wil take any Vector, VectorChild or VectorChildChild object.
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
Joanna Carter [TeamB] schrieb:
"g?khan" <g.*******@gmail.com> a écrit dans le message de news:
11**********************@c74g2000cwc.googlegroups. com...
If you are dealing with reference types, then you are not unboxing, you are
simply casting.
// a base class
class Vector{};
// inherited class
class VectorChild:Vector{};
class VectorChildChild:VectorChild{};
// some other class
class A
{
public void vectorfun(Vector a,Vector b){}
public override void fun(object a,object b) {
// ***********************
// my ugly solution....
// ***********************
if (typeof(Vector) == a.GetType() )
vectorfun( (Vector) a , (Vector) b); // unboxing to Vector
if (typeof(VectorChild) == a.GetType() )
vectorfun( (VectorChild) a , (VectorChild) b); // unboxing to
VectorChild
if (typeof(VectorChildChild) == a.GetType() )
vectorfun( (VectorChildChild) a , (VectorChildChild) b); //
unboxing to VectorChildChild
// ***********************
// ***********************
}
}
I would like to call the "fun" member of an instance of A
using Vector instances and VectorChild instances.
Then why have you got a method fun that takes objects, why not pass the
Vector/derivatives to the strongly typed method ?
public void vectorfun(Vector a,Vector b){}
...wil take any Vector, VectorChild or VectorChildChild object.
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
class A{
public virtual void fun(object a,object b) {}
}
class A0:A{
public override void fun(object a,object b) {}
}
A.fun(a,b) -> problem !
Because I am calling some base class.. That is obviously not my
problem.
这篇关于拆箱的替代方案..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!