繁体   English   中英

从 windows 表单控件复制和粘贴到 WPF 控件时出现序列化错误

[英]Serialization error when copying and pasting from a windows form control to a WPF control

我们有一个可序列化的 class 保存剪贴板中的数据以从 windows 从控制传递到 WPF 控制。 这在转换为.Net 5 后在框架 4.8 中有效,我们现在得到错误:在程序集“System.Private.CoreLib,版本=5.0.0.0,文化=中性,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; }
    }
}
  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM