diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 78f6a5ddbdd989..fcd3398d24589e 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -818,10 +818,15 @@ + + + + + diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/MinimumOSPlatformAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/MinimumOSPlatformAttribute.cs new file mode 100644 index 00000000000000..86df4b11d47f29 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/MinimumOSPlatformAttribute.cs @@ -0,0 +1,32 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Runtime.Versioning +{ + /// + /// Records the operating system (and minimum version) that supports an API. Multiple attributes can be + /// applied to indicate support on multiple operating systems. + /// + /// + /// Callers can apply a + /// or use guards to prevent calls to APIs on unsupported operating systems. + /// + /// A given platform should only be specified once. + /// + [AttributeUsage(AttributeTargets.Assembly | + AttributeTargets.Class | + AttributeTargets.Constructor | + AttributeTargets.Event | + AttributeTargets.Method | + AttributeTargets.Module | + AttributeTargets.Property | + AttributeTargets.Struct, + AllowMultiple = true, Inherited = false)] + public sealed class MinimumOSPlatformAttribute : OSPlatformAttribute + { + public MinimumOSPlatformAttribute(string platformName) : base(platformName) + { + } + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/OSPlatformAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/OSPlatformAttribute.cs new file mode 100644 index 00000000000000..7a91110132aa05 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/OSPlatformAttribute.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Runtime.Versioning +{ + /// + /// Base type for all platform-specific API attributes. + /// +#pragma warning disable CS3015 // Type has no accessible constructors which use only CLS-compliant types + public abstract class OSPlatformAttribute : Attribute +#pragma warning restore CS3015 + { + private protected OSPlatformAttribute(string platformName) + { + PlatformName = platformName; + } + public string PlatformName { get; } + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/ObsoletedInOSPlatformAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/ObsoletedInOSPlatformAttribute.cs new file mode 100644 index 00000000000000..0deaedcbc4ac6f --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/ObsoletedInOSPlatformAttribute.cs @@ -0,0 +1,36 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Runtime.Versioning +{ + /// + /// Marks APIs that were obsoleted in a given operating system version. + /// + /// Primarily used by OS bindings to indicate APIs that should only be used in + /// earlier versions. + /// + [AttributeUsage(AttributeTargets.Assembly | + AttributeTargets.Class | + AttributeTargets.Constructor | + AttributeTargets.Event | + AttributeTargets.Method | + AttributeTargets.Module | + AttributeTargets.Property | + AttributeTargets.Struct, + AllowMultiple = true, Inherited = false)] + public sealed class ObsoletedInOSPlatformAttribute : OSPlatformAttribute + { + public ObsoletedInOSPlatformAttribute(string platformName) : base(platformName) + { + } + + public ObsoletedInOSPlatformAttribute(string platformName, string message) : base(platformName) + { + Message = message; + } + + public string? Message { get; } + public string? Url { get; set; } + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/RemovedInOSPlatformAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/RemovedInOSPlatformAttribute.cs new file mode 100644 index 00000000000000..a3e4e338c04e3f --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/RemovedInOSPlatformAttribute.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Runtime.Versioning +{ + /// + /// Marks APIs that were removed in a given operating system version. + /// + /// + /// Primarily used by OS bindings to indicate APIs that are only available in + /// earlier versions. + /// + [AttributeUsage(AttributeTargets.Assembly | + AttributeTargets.Class | + AttributeTargets.Constructor | + AttributeTargets.Event | + AttributeTargets.Method | + AttributeTargets.Module | + AttributeTargets.Property | + AttributeTargets.Struct, + AllowMultiple = true, Inherited = false)] + public sealed class RemovedInOSPlatformAttribute : OSPlatformAttribute + { + public RemovedInOSPlatformAttribute(string platformName) : base(platformName) + { + } + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/TargetPlatformAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/TargetPlatformAttribute.cs new file mode 100644 index 00000000000000..ce148e2f4e84c3 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/TargetPlatformAttribute.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Runtime.Versioning +{ + /// + /// Records the platform that the project targeted. + /// + [AttributeUsage(AttributeTargets.Assembly, + AllowMultiple = false, Inherited = false)] + public sealed class TargetPlatformAttribute : OSPlatformAttribute + { + public TargetPlatformAttribute(string platformName) : base(platformName) + { + } + } +} diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 6caea918b33e01..dc3d863e359a5b 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -9913,6 +9913,31 @@ public FrameworkName(string identifier, System.Version version, string? profile) public static bool operator !=(System.Runtime.Versioning.FrameworkName? left, System.Runtime.Versioning.FrameworkName? right) { throw null; } public override string ToString() { throw null; } } + [System.AttributeUsageAttribute(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Event | System.AttributeTargets.Method | System.AttributeTargets.Module | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple = true, Inherited = false)] + public sealed class MinimumOSPlatformAttribute : System.Runtime.Versioning.OSPlatformAttribute + { + public MinimumOSPlatformAttribute(string platformName) : base(platformName) { } + } + [System.AttributeUsageAttribute(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Event | System.AttributeTargets.Method | System.AttributeTargets.Module | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple = true, Inherited = false)] + public sealed class ObsoletedInOSPlatformAttribute : System.Runtime.Versioning.OSPlatformAttribute + { + public ObsoletedInOSPlatformAttribute(string platformName) : base(platformName) { } + public ObsoletedInOSPlatformAttribute(string platformName, string message) : base(platformName) { } + public string? Message { get; } + public string? Url { get; set; } + } +#pragma warning disable CS3015 // Type has no accessible constructors which use only CLS-compliant types + public abstract class OSPlatformAttribute : System.Attribute +#pragma warning restore CS3015 // Type has no accessible constructors which use only CLS-compliant types + { + private protected OSPlatformAttribute(string platformName) { } + public string PlatformName { get; } + } + [System.AttributeUsageAttribute(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Event | System.AttributeTargets.Method | System.AttributeTargets.Module | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple = true, Inherited = false)] + public sealed class RemovedInOSPlatformAttribute : System.Runtime.Versioning.OSPlatformAttribute + { + public RemovedInOSPlatformAttribute(string platformName) : base(platformName) { } + } [System.AttributeUsageAttribute(System.AttributeTargets.Constructor | System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited=false)] [System.Diagnostics.ConditionalAttribute("RESOURCE_ANNOTATION_WORK")] public sealed partial class ResourceConsumptionAttribute : System.Attribute @@ -9947,6 +9972,11 @@ public TargetFrameworkAttribute(string frameworkName) { } public string? FrameworkDisplayName { get { throw null; } set { } } public string FrameworkName { get { throw null; } } } + [System.AttributeUsageAttribute(System.AttributeTargets.Assembly, AllowMultiple = false, Inherited = false)] + public sealed class TargetPlatformAttribute : System.Runtime.Versioning.OSPlatformAttribute + { + public TargetPlatformAttribute(string platformName) : base(platformName) { } + } public static partial class VersioningHelper { public static string MakeVersionSafeName(string? name, System.Runtime.Versioning.ResourceScope from, System.Runtime.Versioning.ResourceScope to) { throw null; } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj index a565661a5f4016..548ac964d8584d 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj @@ -221,6 +221,7 @@ + @@ -278,4 +279,4 @@ - + \ No newline at end of file diff --git a/src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs b/src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs new file mode 100644 index 00000000000000..e642e946cfc53b --- /dev/null +++ b/src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs @@ -0,0 +1,55 @@ +using System.Diagnostics.CodeAnalysis; +using Xunit; + +namespace System.Runtime.Versioning.Tests +{ + public class OSPlatformAttributeTests + { + [Theory] + [InlineData("Windows10.0")] + [InlineData("iOS")] + [InlineData("")] + public void TestTargetPlatformAttribute(string platformName) + { + var tpa = new TargetPlatformAttribute(platformName); + + Assert.Equal(platformName, tpa.PlatformName); + } + + [Theory] + [InlineData("Windows8.0", "Obsolete", "http://test.com/obsoletedInOSPlatform")] + [InlineData("Linux", "Message", null)] + [InlineData("iOS13", null, null)] + [InlineData("", null, "http://test.com/obsoletedInOSPlatform")] + public void TestObsoletedInOSPlatformAttribute(string platformName, string message, string url) + { + var opa = message == null ? new ObsoletedInOSPlatformAttribute(platformName) { Url = url} : new ObsoletedInOSPlatformAttribute(platformName, message) { Url = url }; + + Assert.Equal(platformName, opa.PlatformName); + Assert.Equal(message, opa.Message); + Assert.Equal(url, opa.Url); + } + + [Theory] + [InlineData("Windows8.0")] + [InlineData("Android4.1")] + [InlineData("")] + public void TestRemovedInOSPlatformAttribute(string platformName) + { + var tpa = new RemovedInOSPlatformAttribute(platformName); + + Assert.Equal(platformName, tpa.PlatformName); + } + + [Theory] + [InlineData("Windows10.0")] + [InlineData("OSX")] + [InlineData("")] + public void TestMinimumOSPlatformAttribute(string platformName) + { + var tpa = new MinimumOSPlatformAttribute(platformName); + + Assert.Equal(platformName, tpa.PlatformName); + } + } +}