Skip to content

Instantly share code, notes, and snippets.

@emoacht
Created August 5, 2022 23:50
Show Gist options
  • Save emoacht/a942a3de817b6fe661fd7aa12df85954 to your computer and use it in GitHub Desktop.
Save emoacht/a942a3de817b6fe661fd7aa12df85954 to your computer and use it in GitHub Desktop.
Store ValueTuple or Tuple on the Clipboard and retrieve it.
// using System.Windows;
public class TupleTest
{
public void TupleClipboardTest1()
{
(int, string) original = (11, "Eleven");
Clipboard.SetData(DataFormats.Serializable, original);
object retrieved = Clipboard.GetData(DataFormats.Serializable);
Debug.WriteLine(retrieved.GetType()); // System.ValueTuple`2[System.Int32,System.String]
(int, string) restored = (ValueTuple<int, string>)retrieved;
Debug.WriteLine(restored.Item1); // 11
Debug.WriteLine(restored.Item2); // Eleven
}
public void TupleClipboardTest2()
{
Tuple<int, string> original = (11, "Eleven").ToTuple();
Clipboard.SetData(DataFormats.Serializable, original);
object retrieved = Clipboard.GetData(DataFormats.Serializable);
Debug.WriteLine(retrieved.GetType()); // System.Tuple`2[System.Int32, System.String]
Tuple<int, string> restored = (Tuple<int, string>)retrieved;
Debug.WriteLine(restored.Item1); // 11
Debug.WriteLine(restored.Item2); // Eleven
}
public void TupleClipboardTest3()
{
(int, System.Windows.Media.Color) original = (11, System.Windows.Media.Colors.Gray);
Clipboard.SetData(DataFormats.Serializable, original);
object retrieved = Clipboard.GetData(DataFormats.Serializable); // System.Runtime.InteropServices.COMException: 'Data on clipboard is invalid (0x800401D3 (CLIPBRD_E_BAD_DATA))'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment