-
Notifications
You must be signed in to change notification settings - Fork 183
Add information about how the clipboard/dataobject changes in .NET 10 #2129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…mation and the desire to upgrade clipboard code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A number of the samples aren't right. I've commented on some of them. They should be validated. I didn't go through all of them exhaustively, I'll look again after you've updated again and validated that your samples work.
{ | ||
public class BinaryFormatterSupport | ||
{ | ||
public class Person |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BinaryFormatter
serialization requires [Serializable]
.
// Explicit allow-list of permitted types—add only what you need | ||
var allowedTypes = new Dictionary<string, Type> | ||
{ | ||
["MyApp.Person"] = typeof(Person), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These type names don't match the real type names.
namespace ClipboardExamples | ||
{ | ||
// <ITypedDataObjectImplementation> | ||
public class TypedDataObject : DataObject, ITypedDataObject |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DataObject
implements ITypedDataObject
so this isn't a good example. I don't think writing samples for ITypedDataObject
is worth pre-emptively doing.
public static void ModernTryGetDataExample() | ||
{ | ||
// Use this - type-safe approach with TryGetData<T>() | ||
if (Clipboard.TryGetData("MyApp.Person", out Person person)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code isn't particularly helpful without the set/get loop. The true modern way would be to SetDataAsJson
then TryGetData
.
|
||
public static void BrokenCustomTypeExample() | ||
{ | ||
// This worked in .NET 8 and earlier but silently fails starting with .NET 9 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should see a first chance exception under the debugger. The failure happens when OLE tries to make a copy of the data because of the default is copy = true
on SetData
. If it is set to false
it will just work in process as we don't actually have to serialize the type.
// Later retrieval with type safety | ||
if (Clipboard.TryGetData("MyInt", out int value)) | ||
{ | ||
ProcessInteger(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe having the Console.WriteLine
inline would be clearer?
Clipboard.SetData("MyTimeSpan", TimeSpan.FromMinutes(30)); | ||
|
||
// Later retrieval with type safety | ||
if (Clipboard.TryGetData("MyInt", out int value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work. The clipboard only has a single item, which would be "MyTimeSpan"
in this case.
|
||
`BinaryFormatter` was removed from the runtime in .NET 9 because of security vulnerabilities. This change broke clipboard and drag-and-drop operations with custom objects. .NET 10 introduces new APIs that use JSON serialization and type-safe methods to restore this functionality, improve security, and provide better error handling and cross-process compatibility. | ||
|
||
One significant change is that <xref:System.Windows.Clipboard.SetData(System.String,System.Object)?displayProperty=nameWithType> no longer works with custom types. It silently fails without storing data on the clipboard. <xref:System.Windows.Forms.Clipboard.GetData(System.String)?displayProperty=nameWithType> is obsolete in .NET 10 and shouldn't be used, even for built-in types. Use the new <xref:System.Windows.Forms.Clipboard.TryGetData*?displayProperty=nameWithType> and <xref:System.Windows.Forms.Clipboard.SetDataAsJson``1(System.String,``0)?displayProperty=nameWithType> methods for type-safe operations and JSON serialization of custom objects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer works "by default".
To see the failure, you have to catch first chance exceptions under the debugger to see it fail on OLE trying to retrieve a copy of the data (which happens when you set if copy == true
, the default, otherwise on get).
:::code language="json" source="./snippets/how-to-enable-binaryformatter-clipboard-support/csharp/runtimeconfig.json"::: | ||
|
||
> [!IMPORTANT] | ||
> Without this specific runtime switch, clipboard operations won't fall back to `BinaryFormatter` even if general serialization support is enabled. This switch is required specifically for Windows Forms clipboard functionality. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same procedures and switch are used for WPF. If you happen to have both WPF and WinForms in the same project, both Clipboard
classes will be enabled.
Summary
Fixes #2113
Internal previews
Internal previews
Toggle expand/collapse