There is an ICloneable interface which some types can choose to
implement. Types that do usually also make the corresponding Clone()
method part of their public API.
However, it's not universally available because some objects just can't
be cloned. What would it mean to clone a socket? Should it try to open
another connection to the same target? What about the state associated
with the connection?
I gather that ICloneable is to be deprecated though, or at least strongly
discouraged. In general it's hard to implement in a way that fits at all
well with inheritance - it's far to easy to end up in an object slicing
scenario, which isn't good. It's also never too clear what the right
thing is to do wrt. a deep copy - what do you do if only some of the
objects you have references to support cloning?
However, you'll find some objects support it despite the difficulties.
--
Ian Griffiths -
http://www.interact-sw.co.uk/iangblog/
Andrew said:
Thanks for such a quick response Adam. That is certainly a simple and
workable solution. Since you've answered my question the following is
purely academic, maybe you can shed some light. Can a shallow/deep copy
be made of a .net object? Not a copy of my own object but a copy an
object provided by the .net run-time, such as asp.net's response object.
Thanks again for the help.
NameValueCollection source = Request.Form;
NameValueCollection dest = new NameValueCollection();
foreach(string sKey in source.Keys)
dest.Add(sKey, source[sKey]);
I would like to make a copy of the Request.Form collection object. A
shallow copy is fine. Request.Form is read-only and I'd like a copy to
work with. It seems MemberwiseClone() is protected. I'd be very
appreciative if someone would point me in he right direction.
Thank you,
Andrew