Skip to content
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 new properties for Obsolete Attribute #33248

Merged
merged 6 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
2 changes: 2 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2898,7 +2898,9 @@ public ObsoleteAttribute() { }
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
{
Expand Down
26 changes: 16 additions & 10 deletions src/libraries/System.Runtime/tests/System/ObsoleteAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,34 @@ public static void Ctor_Default()
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);
}
}
}