Error while pasting object from Clipboard

3

This is my code for copying and pasting an object to the clipboard:

private void CopyObject()
{
    Clipboard.SetData(ClipboardDataFormats.SampleData, this.SelectedSampleObject);
}

private void PasteObject()
{
    if (Clipboard.ContainsData(ClipboardDataFormats.SampleData) == true)
    {
        var sampleObject = (SampleClass)Clipboard.GetData(ClipboardDataFormats.SampleData); // exception
    }
}

I receive following exception when I call Clipboard.GetData()

System.Runtime.InteropServices.COMException Data on clipboard is invalid.

HRESULT 0x800401D3 (CLIPBRD_E_BAD_DATA)

Maybe the reason for the error is that SelectedSampleObject is an entity framework proxy object, when calling Clipboard.SetData(). Is this possible? Do you have any other idea? Unfortunately i really don't understand this exception.

c#
.net
entity-framework
clipboard
asked on Stack Overflow Dec 7, 2017 by WΩLLE - ˈvɔlə • edited Jun 20, 2020 by Community

1 Answer

2

Not sure about this specific exception, but your code will not work with EF proxy objects anyway, because objects you put to clipboard must be serializable (for example marked with Serializable attribute), and EF proxy classes are not. You can of course disable proxy creation and mark all your entity classes with [Serialiable] but I doubt you want to do this. Instead, create some another class with only required properties, mark it with Serialiable and use that for clipboard operations. Or serialize your object to json\xml whatever yourself and set that to clipboard.

answered on Stack Overflow Dec 7, 2017 by Evk

User contributions licensed under CC BY-SA 3.0