Question

I was wondering if it's possible to create a custom obsolete class. I need it and I hate the fact Obsolete shows up this warning before my input: SOMETHING is Obsolete:. I just want to give a warning/error when I use the field/method with ONLY my input, so for example:

[CustomObsolete("Hello")]
public int i = 0;

Will give warning/debug Hello.

Is this possible? If I use #warning/#error, it will ALWAYS show up the error/warning.

Was it helpful?

Solution

No, ObsoleteAttribute is effectively hard-coded into the C# compiler - there's no way of creating your own attribute that the C# compiler will understand as indicating that a member is obsolete.

Personally I'd just go with ObsoleteAttribute - is it really that bad that the error message starts with "X is obsolete"?

OTHER TIPS

One way to achieve what you want would be a custom post-build task. After your project compiled, let another program that you created load this assembly and parse it for your attribute. You could then output the message you wand and whatever more. But outputting the exact line number and file where this attribute is used will be tricky task, because for this you'd have to parse the pdb file (and would only work in Debug mode properly).

You should better just stick to the Obsolete attribute of .NET. :-)

you can use only obsolete

With Error

    [Obsolete("Hello", true)]
    public String foo() {
        return "bar";
    }

and with warning

    [Obsolete("Hello", false)]
    public String foo() {
        return "bar";
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top