13

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.

  • jon is right,even so...if you want you can check this out http://www.dotnetperls.com/obsolete or http://www.codeproject.com/Articles/107103/Use-Obsolete-attributes-to-indicate-Obsolete-Metho – terrybozzio Jun 12 '13 at 13:11
  • I was wondering it because I want a custom attribute giving out a warning with the input I want. But I think I'll just go with Obsolete. – x_metal_pride_diamond_x Jun 12 '13 at 13:15
  • Maybe you could be interested in this thread for the Roslyn compiler: https://github.com/dotnet/roslyn/issues/156 – NinjaCross Dec 11 '18 at 17:58

3 Answers3

10

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"?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

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. :-)

1

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";
    }
VhsPiceros
  • 410
  • 1
  • 8
  • 17
  • 1
    The way I read it, the OP is aware of ObsoleteAttribute but doesn't like the way that the error is reported - in your case: "foo is obsolete: 'Hello'" – Jon Skeet Jun 12 '13 at 13:11