微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决修改 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 举报,一经查实,本站将立刻删除。