0

Can I assign value to reference type without giving it's reference to that object. Like I have function that gets fileinfo and I wanna check if thats changed later in different thread.

Example

info = GetInfo(path);
checkInfo=info; // I make changes to info later, but I wanna checkInfo to remain the same
Steve
  • 213,761
  • 22
  • 232
  • 286
Siim Nelis
  • 882
  • 7
  • 10
  • 2
    Clone objects: http://ludwigstuyck.wordpress.com/2010/03/03/clone-objects/ – L-Four Mar 28 '13 at 10:12
  • 1
    What is the type of `info` if it is [FileInfo](http://msdn.microsoft.com/en-us/library/system.io.fileinfo.fileinfo.aspx) then you need to deep copy the object, one way would be `checkInfo = new FileInfo(info.FullName);` – Habib Mar 28 '13 at 10:14
  • @Siim Nelis if your problem has been resolved, consider selecting an answer – illegal-immigrant Apr 01 '13 at 12:20

3 Answers3

0

You'll either need to clone the info variable or you'll need to reassign it. Cloning it will be quite a bit of work by hand, and likely fragile, so I'd recommend something like this:

checkInfo = info:
info = GetInfo(path);

one other option, if you didn't change the info variable before you got here, would be to set checkInfo using GetInfo. Consider the fact that if that's the case GetInfo would build the same object for both and thus you're getting the same result.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

As far as I understand your question, you need to create a deep copy of info object

Here's a solution (using serialization)

Create extension method like this:

public static class ExtensionMethods
{
    // Deep clone
    public static T DeepClone<T>(this T a)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, a);
            stream.Position = 0;
            return (T) formatter.Deserialize(stream);
        }
    }
}

Use it:

checkInfo = info.DeepClone();

P.S Usually, a need for a deep cloning means a need for code refactoring. Not necessarily, but quite often..I thing there should be a better solution in your case, than copying objects.

Community
  • 1
  • 1
illegal-immigrant
  • 8,089
  • 9
  • 51
  • 84
  • +1 for the serialization / deserialization approach. I guess one could also use AutoMapper, but I think the serialization approach is safer. – chris Mar 28 '13 at 10:17
  • Of course the tradeoff is that the type has to be serializable, which can be considered a code smell if support for generic cloning is the only reason to make it serializable. – chris Mar 28 '13 at 10:20
  • 1
    Added a P.S section. I totally agree with point about 'code smell'. Generally, there should be no reason for deep copying, only in very specific cases – illegal-immigrant Mar 28 '13 at 10:22
  • Agreed. In fact, whenever you think you need cloning, you should first consider whether a value type instead of a reference type would be more appropriate. – chris Mar 28 '13 at 10:28
  • 1
    The original question might be a good example for your point about code refactoring. Something that is named `GetInfo()` sounds like it returns something that should be treated as immutable anyway. – chris Mar 28 '13 at 10:31
0

You can hide/wrap your FileInfo class in another class and let the other thread access the wrapper class only. The wrapper class can fire an event e.g. FileInfoChanged when the other thread changes the FileInfo.

Siraf
  • 1,133
  • 9
  • 24