wpf icon indicating copy to clipboard operation
wpf copied to clipboard

Expose typed Clipboard APIs in WPF

Open JeremyKuhne opened this issue 8 months ago • 2 comments

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, when one needs to take this dependency for Clipboard or other reasons there are ways to mitigate exposure here.

New Clipboard and DataObject APIs 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<T> set of methods have been introduced to replace the obsoleted APIs
  • Introduces a SetDataAsJson<T> 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

JeremyKuhne avatar Apr 08 '25 18:04 JeremyKuhne

And updates on the index parameter?

miloush avatar Apr 08 '25 20:04 miloush

Is TryGet<T> with T being object compatible with the deprecated APIs?

miloush avatar Apr 08 '25 20:04 miloush