🔒 Locked
[.net] Cloning complex objects (i.e. strings)
Advertisement
Original Post
Hi, I have a remoting systems that creates objects on the server and make them accessible from the clients. Clients need those informations wether the server is running or not, so I thought to make them cloning those objects (mainly strings) locally. My problem is that I don't know how to clone strings (and DateTime)... Is there a way? In addition, our system is already working, because this requirement is a recent one and I would like, if possible, a solution that doesn't require me to rewrite thousands of lines... Thank you!
Thank you, but following the reference String.Clone() simply returns a reference to the string, it does not perform an independent copy. I've found the method Copy(), that should do the job...
As for what concerns DateTime: I didn't know it was a value type, thank you :-)
As for what concerns DateTime: I didn't know it was a value type, thank you :-)
Quote:
Original post by mutex
Why do you need to clone Strings? They are immutable, therefore it's safe to use the same String object in multiple places. That's also why String.Clone() returns itself.
I was wondering the same thing, but didn't want to sound like a fool in case the answer was really obvious.
Quote:
Original post by mutex
Why do you need to clone Strings? They are immutable, therefore it's safe to use the same String object in multiple places. That's also why String.Clone() returns itself.
Because those strings are created on the server. As far as I know on the client side they are not copied, only referenced. That means that if the remote object that created them is not reachable, those strings are not reachable too, and this is not what I want. This is why I want to copy them in order to have a local copy instead than only a reference to something that may or may not be still alive.
Of course, I may very well be wrong about the behaviour of remote references, and if this is the case, please correct me because the code to update would be quite huge and avoid it would save me a lot of time :-)
But aren't you sending them over a network connection anyways? There is no way that the references are preserved over a network connection, I would think.
Anyways, maybe once you have the connection, just write all the strings you need to file, so that the client has a cache somewhere that it can load when the server isn't connected.
Anyways, maybe once you have the connection, just write all the strings you need to file, so that the client has a cache somewhere that it can load when the server isn't connected.
Quote:
Original post by Mike.Popoloski
But aren't you sending them over a network connection anyways? There is no way that the references are preserved over a network connection, I would think.
Anyways, maybe once you have the connection, just write all the strings you need to file, so that the client has a cache somewhere that it can load when the server isn't connected.
For the copy I would use string.Copy(). I suppose that the returned string is in client space. I tried with one of my classes and it works, but I didn't tested it in case of connection lost. Using the file is not an option because there are thousands of these objects coming from the server during the execution of the app because they come from a database.
Quote:
Because those strings are created on the server. As far as I know on the client side they are not copied, only referenced.
One either side of the network, yes, strings are referenced. But to send them across the network, they are decomposed into bytes and shoved into the network stream. The string that the client gets is not a reference to the string on the server. It is a completely different string, that happens to have the same characters in it. You don't need to clone it, nor do you need to clone the string you send from the server.
And once the string "gets there," it is there. If the connection drops, the string will not go away. In effect, there no difference in lifetime or operational behavior between these two strings:
string stringMadeLocally = "Hello, world!";string stringFromNetwork = GetHelloWorldStringFromNetwork();
Quote:
Original post by jpetrie Quote:
Because those strings are created on the server. As far as I know on the client side they are not copied, only referenced.
One either side of the network, yes, strings are referenced. But to send them across the network, they are decomposed into bytes and shoved into the network stream. The string that the client gets is not a reference to the string on the server. It is a completely different string, that happens to have the same characters in it. You don't need to clone it, nor do you need to clone the string you send from the server.
And once the string "gets there," it is there. If the connection drops, the string will not go away. In effect, there no difference in lifetime or operational behavior between these two strings:string stringMadeLocally = "Hello, world!";string stringFromNetwork = GetHelloWorldStringFromNetwork();
Well, if this is the truth, I took a good decision by delaying the update :-)
I thought that the var was just a reference to a proxy object (or something like this) but actually this explains why remotable classes have to be serializable :-)
Thank you very much!
Quote:Classes deriving from MarshalByRef have the marshal-by-reference semantics that you're thinking about. Classes that do not derive from MarshalByRef, including String, have marshal-by-value semantics, which cause such objects to be serialized/deserialized as jpetrie describes.
Original post by cignox1 Quote:
Original post by mutex
Why do you need to clone Strings? They are immutable, therefore it's safe to use the same String object in multiple places. That's also why String.Clone() returns itself.
Because those strings are created on the server. As far as I know on the client side they are not copied, only referenced. That means that if the remote object that created them is not reachable, those strings are not reachable too, and this is not what I want. This is why I want to copy them in order to have a local copy instead than only a reference to something that may or may not be still alive.
Of course, I may very well be wrong about the behaviour of remote references, and if this is the case, please correct me because the code to update would be quite huge and avoid it would save me a lot of time :-)
Quote:
Original post by mutex Quote:Classes deriving from MarshalByRef have the marshal-by-reference semantics that you're thinking about. Classes that do not derive from MarshalByRef, including String, have marshal-by-value semantics, which cause such objects to be serialized/deserialized as jpetrie describes.
Original post by cignox1 Quote:
Original post by mutex
Why do you need to clone Strings? They are immutable, therefore it's safe to use the same String object in multiple places. That's also why String.Clone() returns itself.
Because those strings are created on the server. As far as I know on the client side they are not copied, only referenced. That means that if the remote object that created them is not reachable, those strings are not reachable too, and this is not what I want. This is why I want to copy them in order to have a local copy instead than only a reference to something that may or may not be still alive.
Of course, I may very well be wrong about the behaviour of remote references, and if this is the case, please correct me because the code to update would be quite huge and avoid it would save me a lot of time :-)
Well, this explains many things, but unfortunately a new question arises: my strings are not passed directly. They are instead inside classes that derive from MarshalByRefObject! Are they still copied? I suppose that when I do string a = remoteobject.remotestring the string is copied as described earlier, is that correct?
MSDN says:
Quote:String does not derive from MarshalByRef, therefore it copies. There is no exception for your scenario, plus if there were things would be quite confusing:
Objects that do not inherit from MarshalByRefObject are implicitly marshal by value.
class MyObject : MarshalByRef { public string Data;}string a = "blah";remoteObject.Print(a); // a is copiedMyObject obj = new MyObject();obj.Data = a;// a is now referenced by a MarshalByRef object. should it marshal// by ref or value?remoteObject.Print(a);Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement