Copy of an object.

A

Andrew

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
 
A

Adam Barker

NameValueCollection source = Request.Form;
NameValueCollection dest = new NameValueCollection();

foreach(string sKey in source.Keys)
dest.Add(sKey, source[sKey]);
 
A

Andrew

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.


Adam Barker said:
NameValueCollection source = Request.Form;
NameValueCollection dest = new NameValueCollection();

foreach(string sKey in source.Keys)
dest.Add(sKey, source[sKey]);


Andrew said:
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
 
I

Ian Griffiths [C# MVP]

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.


Adam Barker said:
NameValueCollection source = Request.Form;
NameValueCollection dest = new NameValueCollection();

foreach(string sKey in source.Keys)
dest.Add(sKey, source[sKey]);


Andrew said:
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
 
A

Andrew

Ian thank you for taking the time to explain, this info. is helpful. Just
to be sure I understand correctly... A .net object can be copied (via
facilities provided the .net run-time) only if it exports
..MemberwiseClone(shallow) or adheres to IClonable(deep). Otherwise you can
only get a reference to the object or do a manual copy. Is this correct?

Thanks again.
Andrew




Ian Griffiths said:
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.


Adam Barker said:
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
 
I

Ian Griffiths [C# MVP]

Actually, exporting MemberwiseClone isn't how shallow copies are done.

The ICloneable interface doesn't specify whether the copy is shallow or
deep - it's up to the implementing object to decide what to do. It can do
either, or a hybrid.


--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/

Andrew said:
Ian thank you for taking the time to explain, this info. is helpful. Just
to be sure I understand correctly... A .net object can be copied (via
facilities provided the .net run-time) only if it exports
.MemberwiseClone(shallow) or adheres to IClonable(deep). Otherwise you
can only get a reference to the object or do a manual copy. Is this
correct?

Thanks again.
Andrew




Ian Griffiths said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top