Skip to content

Commit e6bb456

Browse files
buyaa-njeffhandley
andauthored
Add SupportedOSPlatformGuard, UnsupportedOSPlatformGuard attributes (#52028)
* Add SupportedOSPlatformGuard, UnsupportedOSPlatformGuard attributes Co-authored-by: Jeff Handley <jeffhandley@users.noreply.github.com>
1 parent 294fc7b commit e6bb456

File tree

3 files changed

+94
-10
lines changed

3 files changed

+94
-10
lines changed

src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/PlatformAttributes.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,56 @@ public UnsupportedOSPlatformAttribute(string platformName) : base(platformName)
103103
{
104104
}
105105
}
106+
107+
/// <summary>
108+
/// Annotates a custom guard field, property or method with a supported platform name and optional version.
109+
/// Multiple attributes can be applied to indicate guard for multiple supported platforms.
110+
/// </summary>
111+
/// <remarks>
112+
/// Callers can apply a <see cref="System.Runtime.Versioning.SupportedOSPlatformGuardAttribute " /> to a field, property or method
113+
/// and use that field, property or method in a conditional or assert statements in order to safely call platform specific APIs.
114+
///
115+
/// The type of the field or property should be boolean, the method return type should be boolean in order to be used as platform guard.
116+
/// </remarks>
117+
[AttributeUsage(AttributeTargets.Field |
118+
AttributeTargets.Method |
119+
AttributeTargets.Property,
120+
AllowMultiple = true, Inherited = false)]
121+
#if SYSTEM_PRIVATE_CORELIB
122+
public
123+
#else
124+
internal
125+
#endif
126+
sealed class SupportedOSPlatformGuardAttribute : OSPlatformAttribute
127+
{
128+
public SupportedOSPlatformGuardAttribute(string platformName) : base(platformName)
129+
{
130+
}
131+
}
132+
133+
/// <summary>
134+
/// Annotates the custom guard field, property or method with an unsupported platform name and optional version.
135+
/// Multiple attributes can be applied to indicate guard for multiple unsupported platforms.
136+
/// </summary>
137+
/// <remarks>
138+
/// Callers can apply a <see cref="System.Runtime.Versioning.UnsupportedOSPlatformGuardAttribute " /> to a field, property or method
139+
/// and use that field, property or method in a conditional or assert statements as a guard to safely call APIs unsupported on those platforms.
140+
///
141+
/// The type of the field or property should be boolean, the method return type should be boolean in order to be used as platform guard.
142+
/// </remarks>
143+
[AttributeUsage(AttributeTargets.Field |
144+
AttributeTargets.Method |
145+
AttributeTargets.Property,
146+
AllowMultiple = true, Inherited = false)]
147+
#if SYSTEM_PRIVATE_CORELIB
148+
public
149+
#else
150+
internal
151+
#endif
152+
sealed class UnsupportedOSPlatformGuardAttribute : OSPlatformAttribute
153+
{
154+
public UnsupportedOSPlatformGuardAttribute(string platformName) : base(platformName)
155+
{
156+
}
157+
}
106158
}

src/libraries/System.Runtime/ref/System.Runtime.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10334,21 +10334,11 @@ public FrameworkName(string identifier, System.Version version, string? profile)
1033410334
public static bool operator !=(System.Runtime.Versioning.FrameworkName? left, System.Runtime.Versioning.FrameworkName? right) { throw null; }
1033510335
public override string ToString() { throw null; }
1033610336
}
10337-
[System.AttributeUsageAttribute(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Module | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]
10338-
public sealed class SupportedOSPlatformAttribute : System.Runtime.Versioning.OSPlatformAttribute
10339-
{
10340-
public SupportedOSPlatformAttribute(string platformName) : base(platformName) { }
10341-
}
1034210337
public abstract class OSPlatformAttribute : System.Attribute
1034310338
{
1034410339
private protected OSPlatformAttribute(string platformName) { }
1034510340
public string PlatformName { get; }
1034610341
}
10347-
[System.AttributeUsageAttribute(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Module | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]
10348-
public sealed class UnsupportedOSPlatformAttribute : System.Runtime.Versioning.OSPlatformAttribute
10349-
{
10350-
public UnsupportedOSPlatformAttribute(string platformName) : base(platformName) { }
10351-
}
1035210342
[System.AttributeUsageAttribute(System.AttributeTargets.Constructor | System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited=false)]
1035310343
[System.Diagnostics.ConditionalAttribute("RESOURCE_ANNOTATION_WORK")]
1035410344
public sealed partial class ResourceConsumptionAttribute : System.Attribute
@@ -10376,6 +10366,16 @@ public enum ResourceScope
1037610366
Private = 16,
1037710367
Assembly = 32,
1037810368
}
10369+
[System.AttributeUsageAttribute(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Module | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]
10370+
public sealed class SupportedOSPlatformAttribute : System.Runtime.Versioning.OSPlatformAttribute
10371+
{
10372+
public SupportedOSPlatformAttribute(string platformName) : base(platformName) { }
10373+
}
10374+
[System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Method | System.AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
10375+
public sealed class SupportedOSPlatformGuardAttribute : System.Runtime.Versioning.OSPlatformAttribute
10376+
{
10377+
public SupportedOSPlatformGuardAttribute(string platformName) : base(platformName) { }
10378+
}
1037910379
[System.AttributeUsageAttribute(System.AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)]
1038010380
public sealed partial class TargetFrameworkAttribute : System.Attribute
1038110381
{
@@ -10388,6 +10388,16 @@ public sealed class TargetPlatformAttribute : System.Runtime.Versioning.OSPlatfo
1038810388
{
1038910389
public TargetPlatformAttribute(string platformName) : base(platformName) { }
1039010390
}
10391+
[System.AttributeUsageAttribute(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Module | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]
10392+
public sealed class UnsupportedOSPlatformAttribute : System.Runtime.Versioning.OSPlatformAttribute
10393+
{
10394+
public UnsupportedOSPlatformAttribute(string platformName) : base(platformName) { }
10395+
}
10396+
[System.AttributeUsageAttribute(System.AttributeTargets.Field |System.AttributeTargets.Method | System.AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
10397+
public sealed class UnsupportedOSPlatformGuardAttribute : System.Runtime.Versioning.OSPlatformAttribute
10398+
{
10399+
public UnsupportedOSPlatformGuardAttribute(string platformName) : base(platformName) { }
10400+
}
1039110401
public static partial class VersioningHelper
1039210402
{
1039310403
public static string MakeVersionSafeName(string? name, System.Runtime.Versioning.ResourceScope from, System.Runtime.Versioning.ResourceScope to) { throw null; }

src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,27 @@ public void TestSupportedOSPlatformAttribute(string platformName)
4040

4141
Assert.Equal(platformName, tpa.PlatformName);
4242
}
43+
44+
[Theory]
45+
[InlineData("Windows8.0")]
46+
[InlineData("Android4.1")]
47+
[InlineData("")]
48+
public void TestUnsupportedOSPlatformGuardAttribute(string platformName)
49+
{
50+
var uopga = new UnsupportedOSPlatformGuardAttribute(platformName);
51+
52+
Assert.Equal(platformName, uopga.PlatformName);
53+
}
54+
55+
[Theory]
56+
[InlineData("Windows10.0")]
57+
[InlineData("OSX")]
58+
[InlineData("")]
59+
public void TestSupportedOSPlatformGuargAttribute(string platformName)
60+
{
61+
var sopga = new SupportedOSPlatformGuardAttribute(platformName);
62+
63+
Assert.Equal(platformName, sopga.PlatformName);
64+
}
4365
}
4466
}

0 commit comments

Comments
 (0)