Putting the object of userdefined type on clipboard and retriving it

I am having a class Student. I want to store the object of class Student 'objStu' on the clipboard.

Class Student
{
    string stuID;
    string Name;
    string email;
}

In the main method I wrote a code:

Clipboard.SetDataObject(objStu,false);

When I tried to retrive the object as below:

Student anotherStu = Clipboard.GetDataObject() as Student;

It is returning null. Please guide me how to achieve it.

Answers


make Student class as Serializable

[Serializable]
public class Student
{
    public string stuID { get; set; }
    public string Name { get; set; }
    public string email { get; set; }
    public void CopyToClipboard()
    {

        DataFormats.Format format =
             DataFormats.GetFormat(typeof(Student).FullName);
        //now copy to clipboard
        IDataObject dataObj = new DataObject();
        dataObj.SetData(format.Name, false, this);
        Clipboard.SetDataObject(dataObj, false);

    }

then you try as:

Student objStu = new Student();
objStu.Name = "test";
objStu.CopyToClipboard();
Clipboard.SetDataObject(objStu, false);
Student anotherStu = GetFromClipboard();

below is the GetFromClipboard method

protected static Student GetFromClipboard()
{
    Student student = null;
    IDataObject dataObj = Clipboard.GetDataObject();
    string format = typeof(Student).FullName;

    if (dataObj.GetDataPresent(format))
    {
        student = dataObj.GetData(format) as Student;
    }
    return student;
}

Check this link for more information


Need Your Help

Sinatra + Ajax to load dynamic content

ajax jquery web sinatra

I want to create a page with will be filled with dynamic info using Ajax (JQuery). The info will come from various GETs I need to do in other URLs.

Easiest way to select or type over text in a text box in protractor?

protractor

In a protractor test I have an <input type="text"/> that is pre-filled with a value, and I'd like to erase that value and type a new one. Ideally I'd be able to just say something like