dotnet / dotnet/wpf

Expose typed Clipboard APIs in WPF

Open
#10,736 2 comments 0 reactions 0 assignees View on GitHub
area-clipboard
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

As the Windows clipboard is a shared resource and .NET uses binary format (NRBF), there is some amount of risk to having only untyped (`object`) deserialization. While usage of `BinaryFormatter` now requires a [compatibility package](https://learn.microsoft.com/dotnet/standard/serialization/binaryformatter-migration-guide/compatibility-package), when one needs to take this dependency for `Clipboard` or other reasons there are ways to mitigate exposure here.

[New Clipboard and DataObject APIs](https://github.com/dotnet/winforms/issues/12362) describes the new set of APIs and behaviors that both Windows Forms and WPF are implementing in .NET 10.

The brief summary of changes:

- `Get` APIs that return `object` are all obsoleted
- A new `TryGet` set of methods have been introduced to replace the obsoleted APIs
- Introduces a `SetDataAsJson` for simple JSON user type serialization
- `BinaryFormatter` usage in Clipboard scenarios now requires an additional `AppContext` switch

It is recommended that users use .NET primitive types (`string`, `int`, etc.) or arrays or `List<>` of these primitives for `Clipboard` and drag/drop. Simple types can be set with `SetDataAsJson` to avoid `BinaryFormatter`.

Checklist of required work:

- [x] Consume and use shared OLE implementation from WinForms #10544
- [x] Expose typed get and set as JSON APIs #10544
- [ ] Obsolete old APIs
- [ ] Null annotate new APIs in ref assembly
- [ ] Create breaking change documentation
- [ ] Update API documentation

Checklist for other follow-up:

- [ ] Implement additional unit tests
- [ ] Convert drag/drop code to use shared implementation
- [ ] Investigate removing hard dependency of System.Drawing.Common for Clipboard

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.