从windows窗体控件复制和粘贴到WPF控件时发生序列化错误



我们有一个可序列化的类,它保存剪贴板中的数据,以便从windows from控件传递到WPF控件。这在框架4.8中起作用,在转换到.Net 5后,我们现在得到错误:程序集"System.Private.CoreLib,Version=5.0.0.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e"中的类型"System.RuntimeType"未标记为可序列化。

这发生在WPF中的Drop事件处理程序中的行:

var tClip = e.Data.GetDataPresent(typeof(ClipboardDescriptor));

其中"e"是System.Windows.DragEventArgs.

using System;
using System.Windows.Forms;
namespace Support.Classes
{
/// <summary>
/// Summary description for ClipboardDescriptor.
/// </summary>
[Serializable]
public class ClipboardDescriptor
{
private Guid id;
private Guid parentDocumentID;
private System.Type objtype;
private TreeNode baseTreeNode;
private string objname;
public ClipboardDescriptor()
{
baseTreeNode = null;
}
public Guid ParentDocumentID
{
get { return(parentDocumentID); }
set { parentDocumentID = value; }
}
public Guid ID
{
get { return(id); }
set { id = value; }
}
public System.Type ObjType
{
get { return(objtype); }
set { objtype = value; }
}
public string ObjName
{
get { return(objname); }
set { objname = value; }
}
/// <summary>
/// Get the treenode that this object is associated with
/// </summary>
public TreeNode BaseTreeNode
{
get { return(baseTreeNode); }
set { baseTreeNode = value; }
}
}
}

这就是修复。幸运的是,在这种情况下,TreeNode属性是不必要的,并且可以添加TypeName属性,以便从中检索Type。

using System;
namespace JMPT.Support.Classes
{
/// <summary>
/// Summary description for ClipboardDescriptor.
/// </summary>
[Serializable]
public class ClipboardDescriptor
{
public Guid ParentDocumentID { get; set; }
public Guid ID { get; set; }
[field: NonSerialized]
public Type ObjType { get; set; }
public string ObjName { get; set; }
public string ObjTypeName { get; set; }
}
}

最新更新