|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using PolyType.Abstractions; |
| 4 | + |
| 5 | +namespace StreamJsonRpc; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Extension methods for the <see cref="RpcTargetMetadata"/> class. |
| 9 | +/// </summary> |
| 10 | +public static class RpcTargetMetadataExtensions |
| 11 | +{ |
| 12 | +#if NET8_0 |
| 13 | + /// <summary> |
| 14 | + /// A message to use as the argument to <see cref="RequiresDynamicCodeAttribute"/> |
| 15 | + /// for methods that call into <see cref="TypeShapeResolver.ResolveDynamicOrThrow{T}"/>. |
| 16 | + /// </summary> |
| 17 | + /// <seealso href="https://github.com/dotnet/runtime/issues/119440#issuecomment-3269894751"/> |
| 18 | + internal const string ResolveDynamicMessage = |
| 19 | + "Dynamic resolution of IShapeable<T> interface may require dynamic code generation in .NET 8 Native AOT. " + |
| 20 | + "It is recommended to switch to statically resolved IShapeable<T> APIs or upgrade your app to .NET 9 or later."; |
| 21 | +#endif |
| 22 | + |
| 23 | + extension(RpcTargetMetadata) |
| 24 | + { |
| 25 | + /// <summary> |
| 26 | + /// Creates an <see cref="RpcTargetMetadata"/> instance from the specified shape. |
| 27 | + /// </summary> |
| 28 | + /// <typeparam name="T">The type for which a shape should be obtained and <see cref="RpcTargetMetadata"/> generated for.</typeparam> |
| 29 | + /// <returns>An <see cref="RpcTargetMetadata"/> instance initialized from the shape of the <typeparamref name="T"/>.</returns> |
| 30 | +#if NET8_0 |
| 31 | + [RequiresDynamicCode(ResolveDynamicMessage)] |
| 32 | +#endif |
| 33 | +#if NET |
| 34 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 35 | + [Obsolete("Use the RpcTargetMetadata.FromShape<T>() method instead. If using the extension method syntax, check that your type argument actually has a [GenerateShape] attribute or otherwise implements IShapeable<T> to avoid a runtime failure.", error: true)] |
| 36 | +#endif |
| 37 | + public static RpcTargetMetadata FromShape<T>() |
| 38 | + => RpcTargetMetadata.FromShape(TypeShapeResolver.ResolveDynamicOrThrow<T>()); |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Creates an <see cref="RpcTargetMetadata"/> instance from the specified shape. |
| 42 | + /// </summary> |
| 43 | + /// <typeparam name="T">The type for which a shape should be obtained and <see cref="RpcTargetMetadata"/> generated for.</typeparam> |
| 44 | + /// <typeparam name="TProvider">The provider of type shapes from which to obtain the shape.</typeparam> |
| 45 | + /// <returns>An <see cref="RpcTargetMetadata"/> instance initialized from the shape of the <typeparamref name="T"/>.</returns> |
| 46 | +#if NET8_0 |
| 47 | + [RequiresDynamicCode(ResolveDynamicMessage)] |
| 48 | +#endif |
| 49 | +#if NET |
| 50 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 51 | + [Obsolete("Use the RpcTargetMetadata.FromShape<T, TProvider>() method instead. If using the extension method syntax, check that your type argument actually has a [GenerateShape] attribute or otherwise implements IShapeable<T> to avoid a runtime failure.", error: true)] |
| 52 | +#endif |
| 53 | + public static RpcTargetMetadata FromShape<T, TProvider>() |
| 54 | + => RpcTargetMetadata.FromShape(TypeShapeResolver.ResolveDynamicOrThrow<TProvider>()); |
| 55 | + } |
| 56 | +} |
0 commit comments