views:

40

answers:

1

I am working with the clipboard in .net with the following code

   List<object> templateList = new List<object>();
  Clipboard.Clear();


Clipboard.SetDataObject(templateList);
   IDataObject dataObject = Clipboard.GetDataObject();
   var x = (List<object>)dataObject.GetData(typeof(List<object>));

For the above code x is an empty List of objects as you would expect

if i change the code to be

 List<Template> templateList = new List<Template>();
 Clipboard.Clear();
 Clipboard.SetDataObject(templateList);
 IDataObject dataObject = Clipboard.GetDataObject();
 var x = (List<Template>)dataObject.GetData(typeof(List<Template>));

x is now null

the class for Template is both public and Serializable and the application is running on a STAthread

Any ideas?

+1  A: 

Does Template have any object properties that are not marked as serializable? In other words, even though Template is marked as serializable have you actually tried to serialize it, to confirm this works?

barrylloyd
non of the properties have the NonSerialized Attribute, I don't think that you actually need to be able to serialize a object onto the clipboard
Kev Hunter
http://msdn.microsoft.com/en-us/library/cs5ebdfz.aspx states that if you pass a non-serializable object to Clipboard.SetDataObject, it will fail without throwing an exception.So Template must be marked with the SerializableAttribute and any other types in its graph (http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx).Hope this helps!
barrylloyd
I can serialize then deserialize the List<Template> object
Kev Hunter
Ok, sorry I'm not really sure what to try next.I suspect there must be something in the Template class that isn't playing ball with the clipboard (obviously this must be the case, as you say it works ok with List<object>()!)Any chance we can see what the Template class looks like? I might be able to spot something. If not, then perhaps you could try starting with an empty class definition 'Template2' and see if it works, then add in the methods/properties etc of Template one-by-one until it breaks. Yes I know it sounds tedious!
barrylloyd