-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SupportedOSPlatformGuard and UnsupportedOSPlatformGuard Platform-Guard attributes #51541
Comments
We did a long discussion as to whether we should accept a boolean in the manner of We did remove AttributeTargets.Enum from the attribute decl, though. namespace System.Runtime.Versioning
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = false)]
public sealed class SupportedOSPlatformGuardAttribute : OSPlatformAttribute
{
public SupportedOSPlatformGuardAttribute(string platformName) ;
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = false)]
public sealed class UnsupportedOSPlatformGuardAttribute : OSPlatformAttribute
{
public UnsupportedOSPlatformGuardAttribute(string platformName) ;
}
} |
What is the intention with |
Same as |
Sure but what is the intended behaviour in this case [SupportedOSPlatformGuardAttribute("windows")]
public string TestProperty { set { /* does nothing */ } } |
The Keeper of the Guidelines quotes things at you:
But in terms of the actual question, I'm sure the attribute applies only to the get. And maybe the analyzer will complain if it's anything other than a get-only property. |
There's probably a whole set of checks we could do for the guard annotations being misused; perhaps they could be added to #44914 ("Developers get warnings when they misuse [SupportedOSPlatform] or [UnsupportedOSPlatform] attributes"). |
Right, the attributes will only be accounted for a member with boolen type and have a getter, for now, other misuses will be ignored, and yes we could add them into the "Developers get warnings when they misuse [SupportedOSPlatform] or [UnsupportedOSPlatform] attributes" analyzer and warn such cases, I will see where to add it. |
Related to #44922
Background and Motivation
The CA1416 Platform Compatibility analyzer already recognizes platform guards using the methods on
OperatingSystem
, such asOperatingSystem.IsWindows
andOperatingSystem.IsWindowsVersionAtLeast
. However, the analyzer does not recognize other guard methods like a field, property or helper methods that assert platform guards. Expanding this support involves creating new attributes that indicate that an API asserts platform checks the same way the APIs onOperatingSystem
do.Proposed API
Usage Examples
One example is in
Thread
, there is an internal field forIsThreadStartSupported
that indicates whether or not the current platform supports calls toThread.Start
, which is not supported onbrowser
. So in this[UnsupportedOSPlatformGuard]
attributes can be used for this field. And the analyzer will recognize it as a platform guard.Additionally, it's expected that projects will commonly create helper methods that wrap around the
platform-specific API
calls. The platform being guarded using these attributes can be versioned.For guarding multiple platforms need to apply the attribute for each platform
cc @terrajobst @jeffhandley
The text was updated successfully, but these errors were encountered: