深层复制应该有多深? /包含值类型的对象仍然是引用类型吗? [英] How deep should a deep copy go? / Is an object that contains value type still a reference type?

查看:60
本文介绍了深层复制应该有多深? /包含值类型的对象仍然是引用类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在通过ICloneable接口实现Clone()方法

并且我不知道我有多深需要深入复印。


例如:


A类:ICloneable

{

object _val;


// Val将始终为值类型

A(对象值)

{

_val = val;

}


object Clone()

{

//不知道我是否足够

返回新的A(_val);


//我还有去

对象newVal;


if(val.GetType()== typeof(bool))

{

newVal =(bool)val;

}

//....etc....//


返回新的A(newVal)

}

}


因此我必须将对象强制转换回来返回之前的值类型

克隆的实例。


所以我想真正的问题是:是一个对象是con tains值

类型还是参考类型?


谢谢,

Andre

Hi all,

I''m implementing the Clone() method through the ICloneable interface
and don''t quite know how deep I need to go for a deep copy.

Example:

class A: ICloneable
{
object _val;

//Val will always be a value type
A(object val)
{
_val = val;
}

object Clone()
{
//Don''t know if I this is sufficient
return new A(_val);

//Or do I have to go
object newVal;

if(val.GetType() == typeof(bool))
{
newVal = (bool) val;
}
//....etc....//

return new A(newVal)
}
}

Thus do I have to cast the object back to a value type before returning
the cloned instance.

So I suppose the real question is: Is an object that contains value
type still a reference type?

Thanks,
Andre

推荐答案

另外,


如果我需要在创建
$ b $之前将对象强制转换为值类型b新实例,有没有一种自动投射的方法?


一个例子可能会让问题更加清晰:


而不是去:


公共对象CreateNewValueTypeObject(object obj)

{

object newObj;


if(obj.GetType()== typeof(bool))

{

newObj =(bool)obj;

} < br $> b $ b其他....


}


是否可以这样做:


对象newObj =(obj.GetType())obj;


全部谢谢,

Andre


您好


要实现深层复制,您可以使用序列化,这就是实现深度复制

的方式。浅层复制是通过引用完成的,但深层复制意味着

创建一个完整的新对象,简短的实际循环。


一旦我们序列化对象,所有引用也是序列化。


Shivprasad Koirala

C#,VB.NET,SQL SERVER,ASP.NET采访问题
http://www.geocities.com/dotnetinterviews/

Hi

To implement deep copy you can use serialization , thats how deep copy
is implemented. Shallow copy is done by reference , but deep copy means
create a complete new object in short real clooning.

Once we serialize the object all references are also serialzed.

Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions
http://www.geocities.com/dotnetinterviews/


您好Shivprasad,


感谢您的回复。


我搜索了searlization并想出了这个:

http: //weblogs.asp.net/whaggard/arch...3/02/3313.aspx


看起来不错。我想我会尝试一下。


亲切问候,

Andre

Hi Shivprasad,

Thanks for your reply.

I did a search on searlization and came up with this:

http://weblogs.asp.net/whaggard/arch...3/02/3313.aspx

It looks pretty good. I think I''ll give that a try.

Kind regards,
Andre

这篇关于深层复制应该有多深? /包含值类型的对象仍然是引用类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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