Modify property serialization/deserialization in WinForms Form1.resx file

165 Views Asked by At

I have a custom class and made the same serialization as System.Drawing.Image, by implementing ISerializable interface and two methods:

  • void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) to give the serializer my data in the format I need.
  • A constructor in my class: protected MyClass(SerializationInfo info, StreamingContext context)

    [Serializable]
    [TypeConverter(typeof(MyClassTypeConverter))]
    [Editor(typeof(MyClassEditor), typeof(UITypeEditor))]
    public class MyClass : MarshalByRefObject, ISerializable, ICloneable, IDisposable

A have added my class as a property to a custom button -> MyButton.MyClass The problem is that at design time when I set MyClass the serializer writes the assembly name and version in the RESX file of the form before my custom serialized object. Here is how it looks like:


    <assembly alias="MyAssembly" name="MyAssembly, Version=2020.1.1, Culture=neutral, PublicKeyToken=XXX" />
      <data name="myButton1.MyClass" type="MyAssembly.MyClass, MyAssembly">
    <value>Some long text data</data>
      </data>

After a new release my clients started having this exception:


    System.InvalidCastException: '[A]MyAssembly.MyClass cannot be cast to [B]MyAssembly.MyClass. Type A originates from 'MyAssembly, Version=2020.1.1, Culture=neutral, PublicKeyToken=XXX' in the context 'Default' at location 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\...

Here is the designer file:


    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form2));
        this.myButton1 = new MyAssembly.MyButton();
        this.SuspendLayout();
        // 
        // myButton1
        // 
        this.myButton1.Location = new System.Drawing.Point(5, 5);
        this.myButton1.Name = "myButton1";
        this.myButton1.Size = new System.Drawing.Size(200, 25);
        this.myButton1.MyClass = ((MyAssembly.MyClass)(resources.GetObject("myButton1.MyClass")));
        this.myButton1.TabIndex = 0;
        this.myButton1.Text = "myButton1";

The assembly where type MyClass is located is already loaded, because MyButton is located in the same assembly. So I do not need the RESX file to contain information about the assembly. Currently I cannot find a solution on my own and I am thinking of two options:

  1. How can I prevent serializing the <assembly alias="MyAssembly"...> in the resx file of the form?
  2. Can I load MyClass from the resx file without loading the described assembly?
0

There are 0 best solutions below