Jean-David Lanz
Jean-David Lanz

Reputation: 1013

Is there a way to make a C# custom attribute raise a message at compile time?

A colleague of mine is using ObsoleteAttribute on not-yet-implemented methods so we get the warning at compile time.

Since a method to be implemented is in the exact opposite place of an obsolete one, however, that bugs me.

I don't see a NotYetImplementedButPleaseBePatientWeVeGotLoadsOfOtherThingsToDoAsWellAttribute in the documentation, so I'm figuring maybe we could create one.

ObsoleteAttribute is sealed, so we can't inherit it. (Yes, I found that out when I tried it. Nice try, me.)

Is there another way to imitate ObsoleteAttribute, but with a more appropriate name?

Upvotes: 2

Views: 376

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062780

You cannot achieve exactly the same as [Obsolete], because that is handled directly by the compiler. However, you could write a "Roslyn analyzer" that looks for anything you like, and adds warnings, errors, suggestions, etc, based on your preferences. It could perhaps even (perhaps additionally) try to look for throw new NotImplementedException();. Writing a Roslyn analyzer is not, however, a trivial task.

Upvotes: 6

Related Questions