Add RpcTargetMetadata.FromShape<T> overloads that work on .NET Framework#1358
Merged
Add RpcTargetMetadata.FromShape<T> overloads that work on .NET Framework#1358
RpcTargetMetadata.FromShape<T> overloads that work on .NET Framework#1358Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds .NET Framework-compatible overloads of RpcTargetMetadata.FromShape<T> methods using C# 14 extension static methods. The PR enables multi-targeting code to use the same convenient syntax across both .NET and .NET Framework, with the compiler automatically selecting the appropriate implementation based on the target framework.
- Adds extension static methods in a new
RpcTargetMetadataExtensionsclass to provide .NET Framework-compatible overloads - Updates target frameworks to include net9.0 for both the library and tests
- Adds
RequiresUnreferencedCodeattributes to classes using reflection inJsonMessageFormatter.cs
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/StreamJsonRpc/RpcTargetMetadataExtensions.cs | New file containing extension static methods for .NET Framework-compatible FromShape overloads |
| src/StreamJsonRpc/StreamJsonRpc.csproj | Adds net9.0 target framework and suppresses StyleCop SA1201 for new C# 14 extension syntax |
| src/StreamJsonRpc/JsonMessageFormatter.cs | Adds RequiresUnreferencedCode attributes to async enumerable converter classes |
| test/StreamJsonRpc.Tests/StreamJsonRpc.Tests.csproj | Adds net9.0 target framework |
| test/StreamJsonRpc.Tests/RpcTargetMetadataTests.cs | Updates test to use parameterless FromShape<T>() method, removing explicit provider argument |
c72f8cd to
52de5da
Compare
Member
Author
|
docfx is broken by this change. We'll have to block this change on dotnet/docfx#10808 |
f2032c2 to
ef663d4
Compare
matteo-prosperi
approved these changes
Nov 18, 2025
matteo-prosperi
approved these changes
Nov 18, 2025
matteo-prosperi
approved these changes
Nov 18, 2025
aphistra
approved these changes
Nov 18, 2025
matteo-prosperi
approved these changes
Nov 18, 2025
SylviaLe191
approved these changes
Nov 18, 2025
55d7b61 to
80109a4
Compare
matteo-prosperi
approved these changes
Nov 18, 2025
This was referenced Jan 22, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This involves some fancy syntax. Here's an explanation:
The preferred APIs are .NET-only. These are the generic methods with a type constraint of
IShapeable<T>. But this interface has a static member, making it .NET-only.But
FromShape<T>is a convenient method to call, and fast too. We'd like to expose an overload of the method that doesn't have the generic type constraint, since we can implement it properly on .NET Framework. But C# does not let us declare two overloads that vary only by generic type constraint on the same type.But we can declare such overloads when one is on an extension class instead. For instance methods, we could use ordinary extension method syntax. But since the methods to be overloaded are themselves
static, we must use the new C# 14 extension members syntax, which permits extension static methods.This allows multi-targeting code (such as our tests which I made a 1-line change to in order to exercise the new method) to simply use the preferred syntax and the compiler will automatically choose the best method based on the target framework.
The
#if'd attributes on the new methods are to guide users when the compiler doesn't choose the preferred overload on .NET, since when that happens, it is an indication that the code will fail at runtime.The
RequiresDynamicCodeattribute is a subtle requirement of PolyType only on .NET 8. .NET 9+ no longer has that requirement.