Skip to content

Commit

Permalink
Add support for custom string interpolation handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdeago committed Aug 23, 2022
1 parent dcb5c1e commit 0ce1130
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
@@ -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

/// <summary>
/// Indicates which arguments to a method involving an interpolated string handler should be passed to that handler.
/// </summary>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
[ExcludeFromCodeCoverage, DebuggerNonUserCode]
#if POLYKIT_PUBLIC
public
#else
internal
#endif
sealed class InterpolatedStringHandlerArgumentAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="InterpolatedStringHandlerArgumentAttribute"/> class.
/// </summary>
/// <param name="argument">The name of the argument that should be passed to the handler.</param>
/// <remarks><see langword="null"/> may be used as the name of the receiver in an instance method.</remarks>
#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 };
}

/// <summary>
/// Initializes a new instance of the <see cref="InterpolatedStringHandlerArgumentAttribute"/> class.
/// </summary>
/// <param name="arguments">The names of the arguments that should be passed to the handler.</param>
/// <remarks><see langword="null"/> may be used as the name of the receiver in an instance method.</remarks>
public InterpolatedStringHandlerArgumentAttribute(params string[] arguments)
{
Arguments = arguments;
}

/// <summary>Gets the names of the arguments that should be passed to the handler.</summary>
/// <remarks><see langword="null"/> may be used as the name of the receiver in an instance method.</remarks>
public string[] Arguments { get; }
}

#endif
Original file line number Diff line number Diff line change
@@ -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

/// <summary>
/// Indicates the attributed type is to be used as an interpolated string handler.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
[ExcludeFromCodeCoverage, DebuggerNonUserCode]
#if POLYKIT_PUBLIC
public
#else
internal
#endif
sealed class InterpolatedStringHandlerAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="InterpolatedStringHandlerAttribute"/> class.
/// </summary>
public InterpolatedStringHandlerAttribute()
{
}
}

#endif

0 comments on commit 0ce1130

Please sign in to comment.