// 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; }
}
}
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
{
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);
}
}
}