I have a custom type, FooNode, defined in my C# code. I want to add an instance of that custom type to the global System.Windows.Forms.Clipboard, and then retrieve it from the clipboard again. The add seems to work, but I am not able to retrieve the instance. Upon retrieval, several exceptions print to standard output like the following:
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Windows.Forms.dll<br />(... and 9 more just like above)
The result of the retrieval is a null reference. There is no crash or halt. The above exceptions are being dealt with internally; I am not able to catch them. The problem is not with the DataObject itself, because I can retrieve my FooNode from it. I just can't retreive my FooNode from the clipboard's DataObject.
I am able to add and then retreive other types of objects to the system clipboard, such as strings and System.Guid. Why can I not retrieve an object of my custom type?
Following is my test code. Call FooTest.Test() to run.
using System;
using System.Diagnostics;
using System.Windows.Forms;
public class FooNode
{
private Guid m_Guid;
private string m_Name = String.Empty;
public FooNode( )
{
m_Guid = Guid.NewGuid();
m_Name = "Foo";
}
public Guid Guid
{
get { return m_Guid; }
set { m_Guid = value; }
}
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
}
public class FooTest
{
public static void Test( )
{
FooNode fooNode = new FooNode();
DataObject dob = new DataObject( fooNode );
dob.SetData( typeof( Guid ), fooNode.Guid );
dob.SetData( DataFormats.StringFormat, fooNode.Guid.ToString() );
Clipboard.SetDataObject( dob );
object raw = Clipboard.GetDataObject().GetData( typeof( FooNode ) );
Spam( Clipboard.GetDataObject(), new Type[] { typeof( FooNode ), typeof( Guid ) } );
}
public static void Spam( IDataObject dob, Type[] types )
{
Debug.WriteLine( dob );
Debug.Indent();
Debug.WriteLine( "Data formats:" );
Debug.Indent();
string[] formatNames = dob.GetFormats( true );
foreach ( string name in formatNames )
{
Debug.WriteLine( name );
}
Debug.Unindent();
foreach ( string name in formatNames )
{
if ( dob.GetDataPresent( name ) )
{
Debug.WriteLine( String.Format( "Present as format=\"{0}\"", name ) );
Debug.Indent();
object raw = dob.GetData( name );
Debug.WriteLine( String.Format( "raw={0}", raw != null ? raw : "null" ) );
Debug.Unindent();
}
}
if ( types != null )
{
foreach ( Type type in types )
{
if ( dob.GetDataPresent( type ) )
{
Debug.WriteLine( String.Format( "Present as type={0}", type ) );
Debug.Indent();
object raw = dob.GetData( type );
Debug.WriteLine( String.Format( "raw={0}", raw != null ? raw : "null" ) );
Debug.Unindent();
}
}
}
Debug.Unindent();
}
}
modified on Monday, May 18, 2009 8:21 PM