Skip to content

Commit

Permalink
Add CollectionBuilderAttribute polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Nov 22, 2023
1 parent 2e164a2 commit 70f095e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Here's an example of some of the new features that **PolySharp** can enable down
- `[StringSyntax]` (needed to enable [syntax highlight in the IDE](https://github.com/dotnet/runtime/issues/62505))
- `[ModuleInitializer]` (needed to enable [custom module initializers](https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-9.0/module-initializers))
- `[RequiresLocation]` (needed to enable [ref readonly parameters](https://github.com/dotnet/csharplang/issues/6010))
- `[CollectionBuilder]` (needed for [collection expressions](https://github.com/dotnet/csharplang/issues/5354))

To leverage them, make sure to bump your C# language version. You can do this by setting the `<LangVersion>` MSBuild property in your project. For instance, by adding `<LangVersion>11.0</LangVersion>` (or your desired C# version) to the first `<PropertyGroup>` of your .csproj file. For more info on this, [see here](https://sergiopedri.medium.com/enabling-and-using-c-9-features-on-older-and-unsupported-runtimes-ce384d8debb), but remember that you don't need to manually copy polyfills anymore: simply adding a reference to **PolySharp** will do this for you automatically.

Expand Down
1 change: 1 addition & 0 deletions src/PolySharp.Package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Here's an example of some of the new features that **PolySharp** can enable down
- `[StringSyntax]` (needed to enable [syntax highlight in the IDE](https://github.com/dotnet/runtime/issues/62505))
- `[ModuleInitializer]` (needed to enable [custom module initializers](https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-9.0/module-initializers))
- `[RequiresLocation]` (needed to enable [ref readonly parameters](https://github.com/dotnet/csharplang/issues/6010))
- `[CollectionBuilder]` (needed for [collection expressions](https://github.com/dotnet/csharplang/issues/5354))

To leverage them, make sure to bump your C# language version. You can do this by setting the `<LangVersion>` MSBuild property in your project. For instance, by adding `<LangVersion>11.0</LangVersion>` (or your desired C# version) to the first `<PropertyGroup>` of your .csproj file. For more info on this, [see here](https://sergiopedri.medium.com/enabling-and-using-c-9-features-on-older-and-unsupported-runtimes-ce384d8debb), but remember that you don't need to manually copy polyfills anymore: simply adding a reference to **PolySharp** will do this for you automatically.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// <auto-generated/>
#pragma warning disable
#nullable enable annotations

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Runtime.CompilerServices
{
[global::System.AttributeUsage(
global::System.AttributeTargets.Class |
global::System.AttributeTargets.Struct |
global::System.AttributeTargets.Interface,
Inherited = false)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal sealed class CollectionBuilderAttribute : Attribute
{
/// <summary>
/// Initialize the attribute to refer to the <paramref name="methodName"/> method on the <paramref name="builderType"/> type.
/// </summary>
/// <param name="builderType">The type of the builder to use to construct the collection.</param>
/// <param name="methodName">The name of the method on the builder to use to construct the collection.</param>
/// <remarks>
/// <paramref name="methodName"/> must refer to a static method that accepts a single parameter of
/// type <see cref="ReadOnlySpan{T}"/> and returns an instance of the collection being built containing
/// a copy of the data from that span. In future releases of .NET, additional patterns may be supported.
/// </remarks>
public CollectionBuilderAttribute(Type builderType, string methodName)
{
BuilderType = builderType;
MethodName = methodName;
}

/// <summary>
/// Gets the type of the builder to use to construct the collection.
/// </summary>
public Type BuilderType { get; }

/// <summary>
/// Gets the name of the method on the builder to use to construct the collection.
/// </summary>
/// <remarks>
/// This should match the metadata name of the target method.
/// For example, this might be ".ctor" if targeting the type's constructor.
/// </remarks>
public string MethodName { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,5 @@ namespace System.Runtime.CompilerServices
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal sealed class ModuleInitializerAttribute : global::System.Attribute
{
public ModuleInitializerAttribute()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ internal sealed class RequiresPreviewFeaturesAttribute : global::System.Attribut
/// <summary>
/// Initializes a new instance of the <see cref="global::System.Runtime.Versioning.RequiresPreviewFeaturesAttribute"/> class.
/// </summary>
public RequiresPreviewFeaturesAttribute() { }
public RequiresPreviewFeaturesAttribute()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="global::System.Runtime.Versioning.RequiresPreviewFeaturesAttribute"/> class with the specified message.
Expand Down

0 comments on commit 70f095e

Please sign in to comment.