How should I handle an Interning string when deserializing?

In the example below, I am interning a string in the constructor, which is fine. However, when I deserialize an object from binary formatting, I don’t think the string will be interned since the constructor should be called. How do I ensure that the string _name is interned? ... or will it be interned normally?

Edit: This way it works (puts the lines correctly) without processing OnDeserializedAttribute. How it's done?

Am I using a memory profiler, with or without the method below, is it still putting lines? Magic ?: - /

   [OnDeserializedAttribute]
   private void OnDeserialized(StreamingContext context)
   {
       _name = string.Intern(_name);
   }

thank

[Serializable]
class City
{
    private readonly string _name;

    public City(string t)
    {
        _name = string.Intern(t);
    }

    public string Name
    {
        get { return _name; }
    }

    public override string ToString()
    {
        return _name;
    }
}
+5
source share
2 answers

, ISerializable ( ). .

. , - ?

+2

All Articles