Skip to content

Commit

Permalink
Add polyfills for OSPlatformAttribute and its derivatives (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz authored May 23, 2024
1 parent c2ca4e1 commit d9c133d
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 0 deletions.
18 changes: 18 additions & 0 deletions PolyShim/Net50/OSPlatformAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#if (NETCOREAPP && !NET5_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.Versioning;

// https://learn.microsoft.com/en-us/dotnet/api/system.runtime.versioning.osplatformattribute
[ExcludeFromCodeCoverage]
internal abstract class OSPlatformAttribute(string platformName) : Attribute
{
public string PlatformName { get; } = platformName;
}
#endif
31 changes: 31 additions & 0 deletions PolyShim/Net50/SupportedOSPlatformAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#if (NETCOREAPP && !NET5_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.Versioning;

// https://learn.microsoft.com/en-us/dotnet/api/system.runtime.versioning.supportedosplatformattribute
[AttributeUsage(
AttributeTargets.Assembly
| AttributeTargets.Class
| AttributeTargets.Constructor
| AttributeTargets.Enum
| AttributeTargets.Event
| AttributeTargets.Field
| AttributeTargets.Interface
| AttributeTargets.Method
| AttributeTargets.Module
| AttributeTargets.Property
| AttributeTargets.Struct,
AllowMultiple = true,
Inherited = false
)]
[ExcludeFromCodeCoverage]
internal class SupportedOSPlatformAttribute(string platformName)
: OSPlatformAttribute(platformName);
#endif
16 changes: 16 additions & 0 deletions PolyShim/Net50/TargetPlatformAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#if (NETCOREAPP && !NET5_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.Versioning;

// https://learn.microsoft.com/en-us/dotnet/api/system.runtime.versioning.targetplatformattribute
[AttributeUsage(AttributeTargets.Assembly)]
[ExcludeFromCodeCoverage]
internal class TargetPlatformAttribute(string platformName) : OSPlatformAttribute(platformName);
#endif
34 changes: 34 additions & 0 deletions PolyShim/Net50/UnsupportedOSPlatformAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#if (NETCOREAPP && !NET5_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.Versioning;

// https://learn.microsoft.com/en-us/dotnet/api/system.runtime.versioning.unsupportedosplatformattribute
[AttributeUsage(
AttributeTargets.Assembly
| AttributeTargets.Class
| AttributeTargets.Constructor
| AttributeTargets.Enum
| AttributeTargets.Event
| AttributeTargets.Field
| AttributeTargets.Interface
| AttributeTargets.Method
| AttributeTargets.Module
| AttributeTargets.Property
| AttributeTargets.Struct,
AllowMultiple = true,
Inherited = false
)]
[ExcludeFromCodeCoverage]
internal class UnsupportedOSPlatformAttribute(string platformName, string? message = null)
: OSPlatformAttribute(platformName)
{
public string? Message { get; } = message;
}
#endif
29 changes: 29 additions & 0 deletions PolyShim/Net60/SupportedOSPlatformGuardAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#if (NETCOREAPP && !NET6_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.Versioning;

// https://learn.microsoft.com/en-us/dotnet/api/system.runtime.versioning.supportedosplatformguardattribute
[AttributeUsage(
AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property,
AllowMultiple = true,
Inherited = false
)]
[ExcludeFromCodeCoverage]
internal class SupportedOSPlatformGuardAttribute(string platformName)
// OSPlatformAttribute's constructor is not accessible where that type is natively defined
#if !(NETCOREAPP && NET5_0_OR_GREATER)
: OSPlatformAttribute(platformName);
#else
: Attribute
{
public string PlatformName { get; } = platformName;
}
#endif
#endif
29 changes: 29 additions & 0 deletions PolyShim/Net60/UnsupportedOSPlatformGuardAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#if (NETCOREAPP && !NET6_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.Versioning;

// https://learn.microsoft.com/en-us/dotnet/api/system.runtime.versioning.unsupportedosplatformguardattribute
[AttributeUsage(
AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property,
AllowMultiple = true,
Inherited = false
)]
[ExcludeFromCodeCoverage]
internal class UnsupportedOSPlatformGuardAttribute(string platformName)
// OSPlatformAttribute's constructor is not accessible where that type is natively defined
#if !(NETCOREAPP && NET5_0_OR_GREATER)
: OSPlatformAttribute(platformName);
#else
: Attribute
{
public string PlatformName { get; } = platformName;
}
#endif
#endif
45 changes: 45 additions & 0 deletions PolyShim/Net70/ObsoletedOSPlatformAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#if (NETCOREAPP && !NET7_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.Versioning;

// https://learn.microsoft.com/en-us/dotnet/api/system.runtime.versioning.obsoletedosplatformattribute
[AttributeUsage(
AttributeTargets.Assembly
| AttributeTargets.Class
| AttributeTargets.Constructor
| AttributeTargets.Enum
| AttributeTargets.Event
| AttributeTargets.Field
| AttributeTargets.Interface
| AttributeTargets.Method
| AttributeTargets.Module
| AttributeTargets.Property
| AttributeTargets.Struct,
AllowMultiple = true,
Inherited = false
)]
[ExcludeFromCodeCoverage]
internal class ObsoletedOSPlatformAttribute(string platformName, string? message = null)
// OSPlatformAttribute's constructor is not accessible where that type is natively defined
#if !(NETCOREAPP && NET5_0_OR_GREATER)
: OSPlatformAttribute(platformName)
#else
: Attribute
#endif
{
#if (NETCOREAPP && NET5_0_OR_GREATER)
public string PlatformName { get; } = platformName;
#endif

public string? Message { get; } = message;

public string? Url { get; init; }
}
#endif

0 comments on commit d9c133d

Please sign in to comment.