diff --git a/README.md b/README.md index a79fc04..45fea2d 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,8 @@ PolyKit provides support for the following features across all [compatible targe - [System.HashCode](https://docs.microsoft.com/en-us/dotnet/api/system.hashcode) (see note #2); - [SkipLocalsInit](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/general#skiplocalsinit-attribute); - [ValidatedNotNull](https://docs.microsoft.com/en-us/dotnet/api/microsoft.validatednotnullattribute) (see note #3); -- [StackTraceHidden](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stacktracehiddenattribute) (see note #4). +- [StackTraceHidden](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stacktracehiddenattribute) (see note #4); +- support for writing [custom string interpolation handlers](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/interpolated-string-handler). **Note #1:** This feature depends on `System.ValueTuple`, which is not present in .NET Framework versions prior to 4.7. If you reference the `PolyKit.Embedded` package in a project targeting .NET Framework 4.6.2 or 4.6.3, you must also add a package reference to [`System.ValueTuple`](https://www.nuget.org/packages/System.ValueTuple); otherwise, compilation will not fail, but features dependent on ValueTuple will not be present in the compiled assembly. diff --git a/src/PolyKit.Polyfills/System/Runtime/CompilerServices/InterpolatedStringHandlerArgumentAttribute.cs b/src/PolyKit.Polyfills/System/Runtime/CompilerServices/InterpolatedStringHandlerArgumentAttribute.cs new file mode 100644 index 0000000..618fb48 --- /dev/null +++ b/src/PolyKit.Polyfills/System/Runtime/CompilerServices/InterpolatedStringHandlerArgumentAttribute.cs @@ -0,0 +1,57 @@ +#if NET6_0_OR_GREATER + +#if POLYKIT_PUBLIC +[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute))] +#endif + +#else + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace System.Runtime.CompilerServices; + +// https://github.com/dotnet/runtime/blob/v6.0.8/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/InterpolatedStringHandlerArgumentAttribute.cs + +/// +/// Indicates which arguments to a method involving an interpolated string handler should be passed to that handler. +/// +[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] +[ExcludeFromCodeCoverage, DebuggerNonUserCode] +#if POLYKIT_PUBLIC +public +#else +internal +#endif +sealed class InterpolatedStringHandlerArgumentAttribute : Attribute +{ + /// + /// Initializes a new instance of the class. + /// + /// The name of the argument that should be passed to the handler. + /// may be used as the name of the receiver in an instance method. +#pragma warning disable CA1019 // Define accessors for attribute arguments - Preserving original code. + public InterpolatedStringHandlerArgumentAttribute(string argument) +#pragma warning restore CA1019 // Define accessors for attribute arguments + { + Arguments = new string[] { argument }; + } + + /// + /// Initializes a new instance of the class. + /// + /// The names of the arguments that should be passed to the handler. + /// may be used as the name of the receiver in an instance method. + public InterpolatedStringHandlerArgumentAttribute(params string[] arguments) + { + Arguments = arguments; + } + + /// Gets the names of the arguments that should be passed to the handler. + /// may be used as the name of the receiver in an instance method. + public string[] Arguments { get; } +} + +#endif diff --git a/src/PolyKit.Polyfills/System/Runtime/CompilerServices/InterpolatedStringHandlerAttribute.cs b/src/PolyKit.Polyfills/System/Runtime/CompilerServices/InterpolatedStringHandlerAttribute.cs new file mode 100644 index 0000000..cf1b3c0 --- /dev/null +++ b/src/PolyKit.Polyfills/System/Runtime/CompilerServices/InterpolatedStringHandlerAttribute.cs @@ -0,0 +1,38 @@ +#if NET6_0_OR_GREATER + +#if POLYKIT_PUBLIC +[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.CompilerServices.InterpolatedStringHandlerAttribute))] +#endif + +#else + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace System.Runtime.CompilerServices; + +// https://github.com/dotnet/runtime/blob/v6.0.8/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/InterpolatedStringHandlerAttribute.cs + +/// +/// Indicates the attributed type is to be used as an interpolated string handler. +/// +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)] +[ExcludeFromCodeCoverage, DebuggerNonUserCode] +#if POLYKIT_PUBLIC +public +#else +internal +#endif +sealed class InterpolatedStringHandlerAttribute : Attribute +{ + /// + /// Initializes a new instance of the class. + /// + public InterpolatedStringHandlerAttribute() + { + } +} + +#endif