C# 剪贴板 将对象复制到和从
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9032673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clipboard Copying objects to and from
提问by James
I am trying to copy an object onto the windows clipboard and off again. My code is like this:
我正在尝试将对象复制到 Windows 剪贴板并再次关闭。我的代码是这样的:
Copy on to clipboard:
复制到剪贴板:
Clipboard.Clear();
DataObject newObject = new DataObject(prompts);
newObject.SetData(myString);
Clipboard.SetDataObject(newObject);
Where promptsis a List<Data.Sources.PromptResult>collection.
哪里prompts有List<Data.Sources.PromptResult>收藏。
Copy off clipboard:
从剪贴板复制:
IDataObject dataObject = System.Windows.Forms.Clipboard.GetDataObject();
if (dataObject.GetDataPresent(typeof(List<Data.Sources.PromptResult>)))
{
Type type = typeof(List<Data.Sources.PromptResult>);
Object obj = dataObject.GetData(type);
return (List<Data.Sources.PromptResult>)dataObject.GetData(type);
}
The GetFormats()shows the format as being in the list and the GetDataPresent(List<Data.Sources.PromptResult>)returns true but if I try to get the object out of the Clipboardclass with GetData(List<Data.Sources.PromptResult>)I get a return of null.
将GetFormats()格式显示为在列表中,并GetDataPresent(List<Data.Sources.PromptResult>)返回 true,但如果我尝试将对象从Clipboard类中取出,GetData(List<Data.Sources.PromptResult>)我将返回 null。
Does anyone have any idea what might be wrong?
有谁知道可能有什么问题?
采纳答案by Renatas M.
OK I tried to add list of my user type to clipboard and get it back... Here is what I tried:
好的,我尝试将我的用户类型列表添加到剪贴板并将其取回......这是我尝试过的:
My User Class:
我的用户类:
public class User
{
public int Age { get; set; }
public string Name { get; set; }
}
Rest of Code:
其余代码:
// Create User list and add some users
List<User> users = new List<User>();
users.Add(new User { age = 15, name = "Peter" });
users.Add(new User { age = 14, name = "John" });
// Lets say its my data format
string format = "MyUserList";
Clipboard.Clear();
// Set data to clipboard
Clipboard.SetData(format, users);
// Get data from clipboard
List<User> result = null;
if (Clipboard.ContainsData(format))
result = (List<User>)Clipboard.GetData(format);
...and result was null:)
...until I marked Userclass as Serializable
...结果是null:) ...直到我将User班级标记为Serializable
[Serializable]
public class User
{
//...
}
After that my code worked. Ok its not the answer but maybe it helps you some how.
之后我的代码工作。好吧,这不是答案,但也许它可以帮助您一些方法。
回答by granaker
I had a similar scenario and after marking my class as serializable I got it to work.
我有一个类似的场景,在将我的类标记为可序列化后,我让它工作了。
So try putting the Serializable attribute on your class Data.Sources.PromptResult.
因此,尝试将 Serializable 属性放在您的类 Data.Sources.PromptResult 上。
回答by James
@Reniuz thanks for your help it has helped me to work out the answer.
@Reniuz 感谢您的帮助,它帮助我找到了答案。
In order to get the text and custom object data out of the Clipboard with multiple formats I have implemented the IDataObject interface in my own class. The code to set the data object must have the copy flag set like this:
为了以多种格式从剪贴板中获取文本和自定义对象数据,我在自己的类中实现了 IDataObject 接口。设置数据对象的代码必须像这样设置复制标志:
Clipboard.Clear();
Clipboard.SetDataObject(myClassThatImplementsIDataObject, true);
To get the data out again the standard text can be retrieved using:
要再次获取数据,可以使用以下方法检索标准文本:
Clipboard.GetText();
The data can be retrieved using the data method:
可以使用 data 方法检索数据:
Clipboard.GetData("name of my class");
The other point that was helpful was to test that the object I was putting into the clipboard could be serialized by using the BinaryFormatter class to perform this test... If an exception is thrown that copying to the clipboard would also fail, but silently.
另一点很有帮助是测试我放入剪贴板的对象是否可以通过使用 BinaryFormatter 类来序列化来执行此测试...如果抛出异常,复制到剪贴板也会失败,但会无提示。
So my class has:
所以我的班级有:
[Serializable]
public class ClipboardPromptsHolder : IDataObject
{
...
}
回答by Alex Konnen
I found out that, if your class is derived from a different class, the base class also needs to be made [Serializable], otherwise that recipe does not work. In my case, it was something like
我发现,如果您的类是从不同的类派生的,则基类也需要[Serializable],否则该配方不起作用。就我而言,它类似于
public abstract class MyAbstractUser
{
...
}
[Serializable]
public class MyUser : MyAbstractUser
{
...
}
When I tried to exchange values of MyUser over the clipboard, it did not work, but when I added [Serializable] to MyAbstractUser, it did work.
当我尝试通过剪贴板交换 MyUser 的值时,它不起作用,但是当我将 [Serializable] 添加到 MyAbstractUser 时,它起作用了。
回答by JasonG
I came across this while trying to send a list of items to my clipboard. What I needed was the string representation of those items, not the entire object. I tried a few of the suggestions here, to no avail. However, I did come up with my own solution and I wanted to share it. See below.
我在尝试将项目列表发送到剪贴板时遇到了这个问题。我需要的是这些项目的字符串表示,而不是整个对象。我在这里尝试了一些建议,但无济于事。但是,我确实提出了自己的解决方案,我想分享它。见下文。
public static void CopyToClipboard<T>(this IEnumerable<T> items)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (T item in items)
stringBuilder.Append(item.ToString()).AppendLine();
Clipboard.SetText(stringBuilder.ToString());
}
Add this as an extension method and be sure to override your custom Type's ToString() method.
将此添加为扩展方法,并确保覆盖自定义类型的 ToString() 方法。

