Skip to content
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

Add GetData<T> Method to Clipboard #11350

Open
lonitra opened this issue May 10, 2024 · 2 comments
Open

Add GetData<T> Method to Clipboard #11350

lonitra opened this issue May 10, 2024 · 2 comments
Assignees
Labels
api-suggestion (1) Early API idea and discussion, it is NOT ready for implementation area-Clipboard Issues related to Clipboard area-ILLinker/AOT tenet-performance Improve performance, flag performance regressions across core releases
Milestone

Comments

@lonitra
Copy link
Member

lonitra commented May 10, 2024

Currently when grabbing items off the clipboard via Clipboard.GetData we give back type object. It would be good to add typed methods to allow users to express that they are expecting a specific type from the clipboard. This allows us to prevent giving users objects that they do not intend to receive. This will also allow us to avoid fallback to BinaryFormatter when it is not necessary since we would've checked if the requested type matches the root record and avoid the fallback if there is no match.

Note that there are some slightly more specific Clipboard.GetX methods such as GetImage() and GetAudioStream() which internally calls GetData. We would want to update these to call the new GetData<T> as well.

A follow up to this would be to add analyzers that notify users usings the old pattern (SomeType)Clipboard.GetData(); to use GetData<T>() instead

@lonitra lonitra added area-Clipboard Issues related to Clipboard api-suggestion (1) Early API idea and discussion, it is NOT ready for implementation labels May 10, 2024
@lonitra lonitra added this to the .NET 9.0 milestone May 10, 2024
@elachlan
Copy link
Contributor

This is great! Will you also have similar API for SetData<T> and maybe ContainsData<T>? I'd probably lean towards a TryGetData<T> pattern.

I currently use ContainsText and GetText which is then used to create a URI via URI.TryCreate. Unsure if that scenario would work with something like TryGetData<URI>.

@elachlan elachlan added tenet-performance Improve performance, flag performance regressions across core releases area-ILLinker/AOT labels May 10, 2024
@paul1956
Copy link
Contributor

paul1956 commented May 10, 2024

This is great! Will you also have similar API for SetData<T> and maybe ContainsData<T>? I'd probably lean towards a TryGetData<T> pattern.

I currently use ContainsText and GetText which is then used to create a URI via URI.TryCreate. Unsure if that scenario would work with something like TryGetData<URI>.

@elachlan
I think its 2 different applications. If I put text on the clipboard in the form of a valid URI it's still text, if I put a URI on the clipboard its a URI. The question is what happens if I call ContainsData or ContainsText and the clipboard contents is compatible with URI.TryCreate or ToString respectively. I would hope it did the conversion and just worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion (1) Early API idea and discussion, it is NOT ready for implementation area-Clipboard Issues related to Clipboard area-ILLinker/AOT tenet-performance Improve performance, flag performance regressions across core releases
Projects
None yet
Development

No branches or pull requests

4 participants