Clipboard Copying objects to and from

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 User class as Serializable

[Serializable]
public class User
{ 
    //...   
}

After that my code worked.
Ok its not the answer but maybe it helps you some how.

Leave a Comment