修改 WinForms Form1.resx 文件中的属性序列化/反序列化

问题描述

我有一个自定义类并通过实现 ISerializable 接口和两个方法进行了与 System.Drawing.Image 相同的序列化:

  • void ISerializable.GetObjectData(SerializationInfo info,StreamingContext context) 以我需要的格式向序列化程序提供我的数据。
  • 我的类中的一个构造函数:protected MyClass(SerializationInfo info,StreamingContext context)

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

A 已将我的类作为属性添加到自定义按钮 -> MyButton.MyClass 问题是,在设计时,当我设置 MyClass 时,序列化程序会在我的自定义序列化对象之前在表单的 RESX 文件中写入程序集名称和版本。这是它的样子:


    <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>

在新版本发布后,我的客户开始出现此异常:


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

这是设计器文件:


    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";

类型 MyClass 所在的程序集已经加载,因为 MyButton 位于同一个程序集中。所以我不需要 RESX 文件来包含有关程序集的信息。 目前我无法自己找到解决方案,我正在考虑两种选择:

  1. 如何防止序列化表单的 resx 文件中的
  2. 我可以从 resx 文件加载 MyClass 而不加载所描述的程序集吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...