Question

In my DLL I have marked a virtual method as obsolete using System.ObsoleteAttribute but this is not producing the warning which I had expected.

DLL Source (Baz):

[Obsolete("This method has become obsolete, please override `OnEnable` instead.")]
public virtual void OnSelected() {
}

public virtual void OnEnable() {
}

User Source (Foo):

// CS0672 - Doesn't show useful message...
public override void OnSelected() {
}

The following warning is logged upon building the project:

CS0672: Member Foo.OnSelected() overrides obsolete member Baz.OnSelected().
Add the Obsolete attribute to `Foo.OnSelected()'

But I would like my custom obsolete message to appear dominant in this warning so that my customers can follow the provided instructions. Is there a way to achieve this?

Était-ce utile?

La solution

You can add a second parameter to your attribute constructor to tell the compiler to throw a compilation error (rather than the usual "method is obsolete" warning) and the error will use your attribute's message. The error will only occur if the method is called.

[Obsolete("This method has become obsolete, please override `OnEnable` instead.", true)]
public virtual void OnSelected() {
}

Note that this will not remove the CS0672 warning.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top