Custom Compiler Warnings [c#]


Don't know if this will work but it's worth a try.

You can't extend Obsolete, because its final, but maybe you can create your own attribute, and mark that class as obsolete like this:

[Obsolete("Should be refactored")]
public class MustRefactor: System.Attribute{}

Then when you mark your methods with the "MustRefactor" attribute, the compile warnings might show.

I said "maybe" and "might" because I haven't tried this. Please tell me if it doesn't work so I'll remove the answer.


UPDATE: Tested it. It generates a compile time warning, but the error message looks funny, you should see it for yourself and choose. This is very close to what you wanted to achieve.

UPDATE2: With this code It generates this warnings (not very nice, but I don't think there's something better).

public class User
    private String userName;

    [TooManyArgs] // Will show warning: Try removing some arguments
    public User(String userName)
        this.userName = userName;   

    public String UserName
        get { return userName; }
    [MustRefactor] // will show warning: Refactor is needed Here
    public override string ToString()
        return "User: " + userName;
[Obsolete("Refactor is needed Here")]
public class MustRefactor : System.Attribute

[Obsolete("Try removing some arguments")]
public class TooManyArgs : System.Attribute


When using the ObsoleteAtribute in .Net it gives you compiler warnings telling you that the object/method/property is obsolete and somthing else should be used. I'm currently working on a project that requires a lot of refactoring an ex-employees code. I want to write a custom attribute that I can use to mark methods or properties that will generate compiler warnings that give messages that I write. Something like this

[MyAttribute("This code sux and should be looked at")]
public sub DoEverything(){}

I want this to generate a compiler warning that says, "This code sux and should be looked at". I know how to create a custom attribute, the question is how do I cause it to generate compiler warnings in visual studio.

how can I create

I want to create my own attribute [Experimental] which just as [Obsolete] does makes the compiler produce a warning. Is it possible to do this?

No, sorry. Obsolete is a very special attribute. The compiler has special-purpose code written for it. There is no extensible warning mechanism in C#.

If you would like to submit this suggestion to the Visual Studio team, you can do that at

You can add it as an Attribute.

public class Experimental: System.Attribute


Emulate the .NET

Attributes don't actually do anything, they basically just tag the method (or class or whatever) with some metadata. It is the IDE/compiler that does the processing to decide what to do when it sees the Obsolete attribute.

If you wanted visual studio to behave in this way for another attribute, you would have to write a custom plug-in for visual studio.

Can I ask why you want to do this? It sounds like you've got some unimplemented features but you don't want them throwing NotImplementedExceptions? Why not have an empty code block with maybe a //todo: comment to remind yourself that you're not finished?

As an aside, there's a great feature in ReSharper called the 'Todo explorer' which shows (and links) to all of your //todo: comments (and even NotImplementedExceptions)