我有一个自定义类,并通过实现 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, Version=2020.1.1, Culture=neutral, 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 文件来包含有关程序集的信息。目前我无法自己找到解决方案,我正在考虑两种选择:
- 如何防止在表单的 resx 文件中序列化 <assembly alias="MyAssembly"...>?
- 我可以从 resx 文件中加载 MyClass 而不加载所描述的程序集吗?