关于C#剪贴板的问题

woebird woebird 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我想在剪贴板中同时放入对象与文本信息,以便其它程序读取
实现方案如下:
自定义类:
[Serializable]
public class CustomTypeP
{
public bool flag = false;
}
[Serializable]
public class CustomType : CustomTypeP
{
public int i = 0 ;
public string name = "CustomType";
}

在剪贴板中注册自定义类型
[DllImport("User32.dll",CharSet=CharSet.Auto,SetLastError = true)]
public extern static void RegisterClipboardFormat(string formatter);
RegisterClipboardFormat(Type.GetType("Sample.CustomType").ToString());
RegisterClipboardFormat(Type.GetType("Sample.CustomTypeP").ToString());

在剪帖设置对象及信息
IDataObject idata = new DataObject();
idata.SetData(DataFormats.Text, copyContent.ToString());
idata.SetData(Type.GetType("Sample.CustomType").ToString(), new CustomType());
Clipboard.SetDataObject(idata, true);


在本程序中能以下方式读取剪贴 板中的文本与对象:
if(Clipboard.ContainsData(Type.GetType("Sample.CustomType").ToString()))
{
CustomType customType = Clipboard.GetData(Type.GetType("Sample.CustomType").ToString()) as CustomType;
string pasteString = Clipboard.GetText();
}


到此文本与对象都能获得,但是如下自定的父类没有声名序列化即 [Serializable],则获取不是剪贴板中的对象。如果自定义的类继承于系统的类,这个问题应该怎么解决?
...全文
评论 6
收藏 已收藏
分享
返回顶部
woebird
C# 111,128 成员 642,537 内容