-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add platform-specific attributes (#38604)
* Add platform-specific attributes Spec #33331 * Convert to xml doc * Update src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MinimumOSAttribute.cs Co-authored-by: Jeremy Barton <jbarton@microsoft.com> * Update src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ObsoletedInPlatformAttribute.cs Co-authored-by: Jeff Handley <jeffhandley@users.noreply.github.com> * Address code review * Add to ref assembly, test and fix build errors * Fix spacing, revert unwanted changes * Namespace was wrong, updated Co-authored-by: Jeremy Barton <jbarton@microsoft.com> Co-authored-by: Jeff Handley <jeffhandley@users.noreply.github.com> Co-authored-by: Buyaa Namnan <bunamnan@microsoft.com>
- Loading branch information
1 parent
d89772a
commit 9f3e08e
Showing
9 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...raries/System.Private.CoreLib/src/System/Runtime/Versioning/MinimumOSPlatformAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
/// <summary> | ||
/// Records the operating system (and minimum version) that supports an API. Multiple attributes can be | ||
/// applied to indicate support on multiple operating systems. | ||
/// </summary> | ||
/// <remarks> | ||
/// Callers can apply a <see cref="System.Runtime.Versioning.MinimumOSPlatformAttribute" /> | ||
/// or use guards to prevent calls to APIs on unsupported operating systems. | ||
/// | ||
/// A given platform should only be specified once. | ||
/// </remarks> | ||
[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) | ||
{ | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/OSPlatformAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
/// <summary> | ||
/// Base type for all platform-specific API attributes. | ||
/// </summary> | ||
#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; } | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...es/System.Private.CoreLib/src/System/Runtime/Versioning/ObsoletedInOSPlatformAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
/// <summary> | ||
/// 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. | ||
/// </summary> | ||
[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; } | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ries/System.Private.CoreLib/src/System/Runtime/Versioning/RemovedInOSPlatformAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
/// <summary> | ||
/// Marks APIs that were removed in a given operating system version. | ||
/// </summary> | ||
/// <remarks> | ||
/// Primarily used by OS bindings to indicate APIs that are only available in | ||
/// earlier versions. | ||
/// </remarks> | ||
[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) | ||
{ | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...libraries/System.Private.CoreLib/src/System/Runtime/Versioning/TargetPlatformAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
/// <summary> | ||
/// Records the platform that the project targeted. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Assembly, | ||
AllowMultiple = false, Inherited = false)] | ||
public sealed class TargetPlatformAttribute : OSPlatformAttribute | ||
{ | ||
public TargetPlatformAttribute(string platformName) : base(platformName) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |