Skip to content

Commit

Permalink
Add platform-specific attributes (#38604)
Browse files Browse the repository at this point in the history
* 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
4 people authored Jul 1, 2020
1 parent d89772a commit 9f3e08e
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -818,10 +818,15 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\ComponentGuaranteesAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\ComponentGuaranteesOptions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\FrameworkName.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\MinimumOSPlatformAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\ObsoletedInOSPlatformAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\OSPlatformAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\ResourceConsumptionAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\ResourceExposureAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\ResourceScope.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\TargetFrameworkAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\TargetPlatformAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\RemovedInOSPlatformAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\VersioningHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\RuntimeType.cs" Condition="'$(TargetsCoreRT)' != 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\SByte.cs" />
Expand Down
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)
{
}
}
}
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; }
}
}
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; }
}
}
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)
{
}
}
}
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)
{
}
}
}
30 changes: 30 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9914,6 +9914,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
Expand Down Expand Up @@ -9948,6 +9973,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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
<Compile Include="System\Runtime\Serialization\OptionalFieldAttributeTests.cs" />
<Compile Include="System\Runtime\Serialization\SerializationExceptionTests.cs" />
<Compile Include="System\Runtime\Serialization\StreamingContextTests.cs" />
<Compile Include="System\Runtime\Versioning\OSPlatformAttributeTests.cs" />
<Compile Include="System\Security\SecurityAttributeTests.cs" />
<Compile Include="System\Security\SecurityExceptionTests.cs" />
<Compile Include="System\Text\StringBuilderTests.cs" />
Expand Down Expand Up @@ -278,4 +279,4 @@
<ItemGroup>
<PackageReference Include="Moq" Version="$(MoqVersion)" />
</ItemGroup>
</Project>
</Project>
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);
}
}
}

0 comments on commit 9f3e08e

Please sign in to comment.