复制参考类型? [英] Copy Reference Type?

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

问题描述

好的,这应该很简单,但是如何按值复制引用类型?


含义


SomeComplexObject s1 = new SomeComplexObject();

SomeComplexObject s2 = new SomeComplexObject();

s1.color =" red";

s2 = s1;

s2.color =" blue";


我想将s1复制到s2并让它们指向内存中的不同点,

所以如果我更改s2.color我就不会改变s1.color ...我该怎么做?

实际对象有30-soh参数所以我''我希望我没有必要分别为每一个分配


解决方案

Brett,


没有标准的方法可以做到这一点。该框架定义了一个IClonable

接口,用于这样的事情。但是,它不会说

你是否应该做一个深层复制或对象的浅层复制。


最简单的方法这将是实现IClonable,然后

让它返回MemberwiseClone(Object上的受保护方法)返回的内容。

但是,这只是一层深度。如果你的

对象中有对象引用被复制,那么这些对象不会被复制,而是

它们的引用。


如果你想要一个脏方法来做这个,并且对象被标记为

serializable,那么你可以将对象序列化为一个内存流,然后

反序列化它,你有一份很深的副本。


希望这会有所帮助。

-

- Nicholas Paldino [ .NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Brett Wickard" < no **** @ nospam.comwrote in message

news:%2 **************** @ TK2MSFTNGP04.phx.gbl ...


好​​的,这应该很简单,但是如何按值复制引用类型?


含义


SomeComplexObject s1 = new SomeComplexObject();

SomeComplexObject s2 = new SomeComplexObject();

s1.color =" red" ;;

s2 = s1;

s2.color =" blue";


我想将s1复制到s2并拥有他们指向内存中的不同点,

如果我改变s2.color我就不会改变s1.color ...我该怎么做?

实际的对象有30个参数,所以我希望我不必单独分配每个参数!



您可以编写自己的复制或克隆方法来执行此操作,但这就是您需要做的事情。

-

HTH,


凯文斯宾塞

微软MVP

鸡肉沙拉手术


橙色你平淡无奇我停止了香蕉蔓延? br />
" Brett Wickard" < no **** @ nospam.comwrote in message

news:%2 **************** @ TK2MSFTNGP04.phx.gbl ...


好​​的,这应该很简单,但是如何按值复制引用类型?


含义


SomeComplexObject s1 = new SomeComplexObject();

SomeComplexObject s2 = new SomeComplexObject();

s1.color =" red" ;;

s2 = s1;

s2.color =" blue";


我想将s1复制到s2并拥有他们指向内存中的不同点,

如果我改变s2.color我就不会改变s1.color ...我该怎么做?

实际的对象有30个参数,所以我希望我不必单独分配每个参数!



" Brett Wickard" < no **** @ nospam.comwrote in message

news:%2 **************** @ TK2MSFTNGP04.phx.gbl ...


好​​的,这应该很简单,但是如何按值复制引用类型?


含义


SomeComplexObject s1 = new SomeComplexObject();

SomeComplexObject s2 = new SomeComplexObject();

s1.color =" red" ;;

s2 = s1;

s2.color =" blue";


我想将s1复制到s2并拥有他们指向内存中的不同点,

如果我改变s2.color我就不会改变s1.color ...我该怎么做?

实际的对象有30个参数,所以我希望我没有必要分别为每一个分配



是的,你必须写一个复制方法,手动返回你班级的新实例

。你可以做一个浅拷贝使用MemberwiseClone()方法

(从对象继承)但新实例的字段仍然会在内存中引用相同的对象。


执行深层复制的可能方法是序列化和反序列化

实例。


Ok, this should be simple, but how do I copy a reference type by value?

Meaning

SomeComplexObject s1 = new SomeComplexObject ();
SomeComplexObject s2 = new SomeComplexObject ();
s1.color = "red";
s2 = s1;
s2.color = "blue";

I want to copy s1 to s2 and have them point to different points in memory,
so that if I change s2.color I won''t change s1.color... How do I do that?
The actual object has like 30-ish parameters so I''m hoping I don''t have to
assign each one separately!

解决方案

Brett,

There is no standard way to do this. The framework defines an IClonable
interface which is used for things like this. However, it doesn''t say
whether or not you should do a deep copy or a shallow copy of the object.

The simplest way to do this would be to implement IClonable, and then
have it return what MemberwiseClone (a protected method on Object) returns.
However, this is only one layer deep. If you have object references in your
object which is being copied, then those objects are not copied, but rather,
their references.

If you want a dirty way to do this, and the object is marked as
serializable, then you can serialize the object to a memory stream, and then
deserialize it back, and you have a deep copy.

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

"Brett Wickard" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...

Ok, this should be simple, but how do I copy a reference type by value?

Meaning

SomeComplexObject s1 = new SomeComplexObject ();
SomeComplexObject s2 = new SomeComplexObject ();
s1.color = "red";
s2 = s1;
s2.color = "blue";

I want to copy s1 to s2 and have them point to different points in memory,
so that if I change s2.color I won''t change s1.color... How do I do that?
The actual object has like 30-ish parameters so I''m hoping I don''t have to
assign each one separately!



You can write your own Copy or Clone method to do this, but that''s what
you''ll have to do.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
"Brett Wickard" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...

Ok, this should be simple, but how do I copy a reference type by value?

Meaning

SomeComplexObject s1 = new SomeComplexObject ();
SomeComplexObject s2 = new SomeComplexObject ();
s1.color = "red";
s2 = s1;
s2.color = "blue";

I want to copy s1 to s2 and have them point to different points in memory,
so that if I change s2.color I won''t change s1.color... How do I do that?
The actual object has like 30-ish parameters so I''m hoping I don''t have to
assign each one separately!



"Brett Wickard" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...

Ok, this should be simple, but how do I copy a reference type by value?

Meaning

SomeComplexObject s1 = new SomeComplexObject ();
SomeComplexObject s2 = new SomeComplexObject ();
s1.color = "red";
s2 = s1;
s2.color = "blue";

I want to copy s1 to s2 and have them point to different points in memory,
so that if I change s2.color I won''t change s1.color... How do I do that?
The actual object has like 30-ish parameters so I''m hoping I don''t have to
assign each one separately!

Yes, you have to write a copy method returning new instance of your class
manually. You can make a shallow copy usign the MemberwiseClone() method
(inherited from object) but the fields of the new instance will still
reference the same objects in memory.

A possible hack to perform a deep copy is to serialize and deserialize the
instance.


这篇关于复制参考类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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