Created
August 5, 2022 23:50
-
-
Save emoacht/a942a3de817b6fe661fd7aa12df85954 to your computer and use it in GitHub Desktop.
Store ValueTuple or Tuple on the Clipboard and retrieve it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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