.NET: Serializing object to a file from a 3rd party assembly (to make Selenium WebDriver faster)

人走茶凉 提交于 2019-12-03 14:53:44

Create your own custom object that derives from ISerializable and the Object you want to serialize and serialize that custom object. This specific example won't work if the 3rd Party object is Sealed (there are other ways which can still be used with ISerializable).

Updated Per Request

Updated Per Question Update

public class MyFirefoxDriver : FirefoxDriver, ISerializable
{
  public MyFirefoxDriver(<Interface/Class> firefoxProfile)
    :base(firefoxProfile)
  {
  }

  void GetObjectData(SerializationInfo info, StreamingContext context)
  {
    // Properties needing to be serialized
    info.AddValue("SomeProperty", base.SomeProperty);
  }
}

Update 2

Your new code is confusing me. I think you're looking for..

serializedObject = serializer.DeSerializeObject(@"C:\firefoxDriver.qa");
driver = serializedObject;

This is because FirefoxDriverSerialized is a FireFoxDriver.

Update 3

It is important to note that constructors are not called when an object is deserialized. This means that things that are normally constucted/set in the constructor won't be upon deserialization, which usually results in a NullReferenceException. The way around that is to implement ISerializable and explicity set the objects needed for the class to work (both for GetObjectData and the special deserializer constructor). This can be most difficult if the understanding of the object in question is not simple nor if we don't have the source for it.

It is important to stress that you need to implement both GetObjectData as well as the special constructor when ISerializable is added to a class. The compiler will warn you if GetObjectData is missing, but since it is impossible to enforce the implementation of a constructor, no warnings will be given if the constructor is absent and an exception will be thrown when an attempt is made to deserialize a class without the constructor.

public class MyObject
{
  public MyObject()
  {
    this.SomeOtherObject = new MyObject2();
  }
  public string Name { get; set; }
  public MyObject2 SomeOtherObject { get; set; }
}

public class MyObjectSerializable : MyObject, ISerializable
{
  protected MyObjectSerializable(SerializationInfo si, StreamingContext context) 
  {
    // base() is never called during deserialization
    // so use the special ISerializable constructor to set the value of the object
    // why not add it to the si.AddValue?
    // because, most likely in this question, it is not a [Serializable] object either
    // so we have to treat it differently as well
    this.SomeOtherObject = new MyObject2();
  }

  public override void GetObjectData(SerializationInfo si, StreamingContext context)
  {
    si.AddValue("Name", Name);
  }
}

It isn't required to inherit from ISerializable. By default, the object must either inherit from ISerializable or be decorated with the SerializableAttribute. Without either of those, your best option is to use a SurrogateSelector. This will allow you to tell the serializer to serialize another object in its place based on how you define your surrogate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!