So I'm trying to copy and paste an object and having trouble getting it right. I've searched through the topics but I still can't seem to get it to work. Here is the code:
In one solution in Visual studio I have the the class:
namespace test4
{
[Serializable]
public class copypaste
{
public string test = "a";
}
}
and the copy part of the code:
private void btn1_Click(object sender, EventArgs e)
{
var copy_obj = new copypaste();
DataObject d = new DataObject(copy_obj);
Clipboard.SetDataObject(d);
}
And in another solution I have:
namespace test4
{
[Serializable]
public class copypaste
{
public string test = "a";
}
}
and the paste part of the code:
private void btnTest_Click(object sender, EventArgs e)
{
var d = Clipboard.GetDataObject();
if (d.GetDataPresent("test4.copypaste"))
{
var o = d.GetData("test4.copypaste");
Debug.WriteLine( ( (copypaste)o ).test );
}
}
However, I end up with the following error on the final line: 'System.InvalidCastException: 'Unable to cast object of type 'System.IO.MemoryStream' to type 'test4.copypaste'.'
I have gone through other questions which suggest this way of copy/pasting code but none seem to return memory stream when they call the GetData method. I am unsure how to extract the object from the memory stream.
Thanks