开发者

C# Reset a property

I'm writing a Clone function for a non serializeable object.For most objects I don't care if they are shallow copied as I won't be making any changes to them.

I start with a M开发者_JAVA技巧emberwiseClone and this copies all the values and few objects like configuration dictionary over just fine but they are pointers.

EAVEntity newClone = (EAVEntity) this.MemberwiseClone();
newClone.EntityStorageID = Guid.NewGuid();
newClone.Controls.Clear();

So how do I reset a pointer so I can make them not point at the same location?


To be precise, you are not working with "pointers" in their true sense but rather with objects that are reference types. Quite a difference.

If you want the property of your copied object not to point to the same other object, you could either set it to null, a new or any other object for that matter:

newClone.SomeProperty = null;

newClone.SomeProperty = new WhatEverTypeSomePropertyIs();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜