关于C#剪贴板的问题
我想在剪贴板中同时放入对象与文本信息,以便其它程序读取
实现方案如下:
自定义类:
[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],则获取不是剪贴板中的对象。如果自定义的类继承于系统的类,这个问题应该怎么解决?