I'd like to use a ObsoleteAttribute
with a custom const message in order to avoid copy/pasting the message everywhere.
For example here is my class:
[AttributeUsage(AttributeTargets.Property)]
public class MyObsoleteAttribute : Attribute
{
private const string CustomMessage = "This is a custom warning message";
public MyObsoleteAttribute()
{
Console.WriteLine(CustomMessage);
}
}
public class MyClass
{
[MyObsolete]
public string? Foo { get; set; }
[MyObsolete]
public string? Bar { get; set; }
[MyObsolete]
public string? FooBar { get; set; }
}
I'd like to receive a warning in JetBrains Rider (or Visual Studio) when using such property in the same way as the Obsolete
attribute with my CustomMessage
.
I try overriding ObsoleteAttribute
but it is sealed
.
For example the following code should raise a warning (compile time) telling me "This is a custom warning message".
var myClass = new MyClass();
var foo = myClass.Foo;
var bar = myClass.Bar;
var fooBar = myClass.FooBar;
I looked here but I couldn't find exactly an answer to this case: the closest answer is this but it gives me warning on the property and not when using them.
You could define a string constant
public class ObsoleteReasons
{
public const string Reason1 = "Don't use this";
}
and use it in the attribute
[Obsolete(ObsoleteReasons.Reason1)]