From: buyaa-n Date: Fri, 6 Mar 2020 00:46:50 +0000 (-0800) Subject: Add new properties for Obsolete Attribute (#33248) X-Git-Tag: submit/tizen/20210909.063632~9315 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0c042417e9a725d4fe0481b258c87fa36443646f;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Add new properties for Obsolete Attribute (#33248) * Add DiagnosticId UrlFormat propertites for Obsolete attribute --- diff --git a/src/libraries/System.Private.CoreLib/src/System/ObsoleteAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/ObsoleteAttribute.cs index 94a26dd9507..0bd3af61b8a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ObsoleteAttribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ObsoleteAttribute.cs @@ -18,35 +18,39 @@ namespace System // Error indicates if the compiler should treat usage of such a method as an // error. (this would be used if the actual implementation of the obsolete // method's implementation had changed). + // DiagnosticId. Represents the ID the compiler will use when reporting a use of the API. + // UrlFormat.The URL that should be used by an IDE for navigating to corresponding documentation. Instead of taking the URL directly, + // the API takes a format string. This allows having a generic URL that includes the diagnostic ID. // [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Delegate, Inherited = false)] public sealed class ObsoleteAttribute : Attribute { - private readonly string? _message; - private readonly bool _error; - public ObsoleteAttribute() { - _message = null; - _error = false; + Message = null; + IsError = false; } public ObsoleteAttribute(string? message) { - _message = message; - _error = false; + Message = message; + IsError = false; } public ObsoleteAttribute(string? message, bool error) { - _message = message; - _error = error; + Message = message; + IsError = error; } - public string? Message => _message; + public string? Message { get; } + + public bool IsError { get; } + + public string? DiagnosticId { get; set; } - public bool IsError => _error; + public string? UrlFormat { get; set; } } } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index efd42520dc7..467d544051f 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -2898,7 +2898,9 @@ namespace System public ObsoleteAttribute(string? message) { } public ObsoleteAttribute(string? message, bool error) { } public bool IsError { get { throw null; } } + public string? DiagnosticId { get { throw null; } set { } } public string? Message { get { throw null; } } + public string? UrlFormat { get { throw null; } set { } } } public sealed partial class OperatingSystem : System.ICloneable, System.Runtime.Serialization.ISerializable { diff --git a/src/libraries/System.Runtime/tests/System/ObsoleteAttributeTests.cs b/src/libraries/System.Runtime/tests/System/ObsoleteAttributeTests.cs index 99f6c31831f..af3bb7cd1f7 100644 --- a/src/libraries/System.Runtime/tests/System/ObsoleteAttributeTests.cs +++ b/src/libraries/System.Runtime/tests/System/ObsoleteAttributeTests.cs @@ -14,28 +14,34 @@ namespace System.Tests var attribute = new ObsoleteAttribute(); Assert.Null(attribute.Message); Assert.False(attribute.IsError); + Assert.Null(attribute.DiagnosticId); + Assert.Null(attribute.UrlFormat); } [Theory] - [InlineData(null)] - [InlineData("")] - [InlineData("message")] - public void Ctor_String(string message) + [InlineData(null, null)] + [InlineData("", "BCL0006")] + [InlineData("message", "")] + public void Ctor_String_Id(string message, string id) { - var attribute = new ObsoleteAttribute(message); + var attribute = new ObsoleteAttribute(message) { DiagnosticId = id }; Assert.Equal(message, attribute.Message); Assert.False(attribute.IsError); + Assert.Equal(id, attribute.DiagnosticId); + Assert.Null(attribute.UrlFormat); } [Theory] - [InlineData(null, true)] - [InlineData("", false)] - [InlineData("message", true)] - public void Ctor_String_Bool(string message, bool error) + [InlineData(null, true, "")] + [InlineData("", false, null)] + [InlineData("message", true, "https://aka.ms/obsolete/{0}")] + public void Ctor_String_Bool_Url(string message, bool error, string url) { - var attribute = new ObsoleteAttribute(message, error); + var attribute = new ObsoleteAttribute(message, error) { UrlFormat = url }; Assert.Equal(message, attribute.Message); Assert.Equal(error, attribute.IsError); + Assert.Null(attribute.DiagnosticId); + Assert.Equal(url, attribute.UrlFormat); } } }