1
Vote

Static cache issue...

description

Hi,

I am using serialization in different use case in the same process.
Sometime I want to exclude a member of a class in a given serialization and in other occasion, I want to include it. The cache in PropertyProvider is preventing me to use the serializer in that way.

For a simple test, I used :
SharpSerializer serializer1 = new SharpSerializer();
serializer1.Serialize(anObject, @"c:\File1.txt");

var settingsnoattrib = new SharpSerializerXmlSettings();
settingsnoattrib.AdvancedSettings.AttributesToIgnore.Add(typeof(MyExcludeAttribute));
SharpSerializer serializer2 = new SharpSerializer(settingsnoattrib);
serializer2.Serialize(anObject, @"c:\File2.txt");
File1 and File2 will be identical and that an issue for me.
If I reverse the order of the code, the attribute will be removed in both serialization.

I changed the code to add a Clear of the cache when the _attributesToIgnore (PropertyProvider.cs) is changed, but since its done at each construction of the Sharserializer, I am losing the cache optimization. I could add more code to help but before ....

Is there a way to your released Sharpserializer to answer my use case ?

thx

comments

polo wrote Aug 29, 2014 at 12:05 PM

Indeed there is bug in caching properties. As a workaround I would suggest creating a wrapper around SharpSerializer which instantiates SharpSerializer each time the serialization is needed. The wrapper can be a class or a method.

jstrembl wrote Aug 29, 2014 at 8:31 PM

I tried this and it worked ...
Any better suggestions on how to do it is welcome !
thx
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class MyExcludeAttribute : Attribute
{
}
public class Testobject
{
  public int Patate { get; set; }
  public string Poil { get; set; }
  [MyExcludeAttribute]
  public string Pitou { get; set; }
}

public class SimpleSerializer
{
  SharpSerializer serializer1 = new SharpSerializer();

  public void SerializeObject(Object iObject)
  {
    serializer1.Serialize(iObject, @"c:\File1.txt");
  }
}

public class SimpleSerializerWithExclude
{
  SharpSerializer serializer2;

  public SimpleSerializerWithExclude()
  {
    var settingsnoattrib = new SharpSerializerXmlSettings();
    settingsnoattrib.AdvancedSettings.AttributesToIgnore.Add(typeof(MyExcludeAttribute));
    serializer2 = new SharpSerializer(settingsnoattrib);
  }

  public void SerializeObject(Object iObject)
  {
     serializer2.Serialize(iObject, @"c:\File1.txt");
  }
}

class Program
{
  static void Main(string[] args)
  {
    Testobject anObject = new Testobject();
    SimpleSerializer wSimple = new SimpleSerializer();
    SimpleSerializerWithExclude wSimpleWithExclude = new SimpleSerializerWithExclude();

    wSimple.SerializeObject(anObject);
    wSimpleWithExclude.SerializeObject(anObject);
  }
}

jstrembl wrote Aug 31, 2014 at 6:08 PM

There was an issue with the above code (using same output file name for both serialization).
I don't see how to get around the static cache even with a wrapper that recreates the SharpSerializer class at every serialization. Unless cleared, the cache will always remain.

Why not having that member not static ? I understand that there would be a loss of performance if the SharpSerializer class is instantiated, but a proper usage would then to keep the class instantiated for each usage.

Let me know.
thx