Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# custom obsolete attribute

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.

like image 509
x_metal_pride_diamond_x Avatar asked Jun 12 '13 13:06

x_metal_pride_diamond_x


2 Answers

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

like image 195
JustAnotherUserYouMayKnow Avatar answered Oct 04 '22 13:10

JustAnotherUserYouMayKnow


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";
    }
like image 37
VhsPiceros Avatar answered Oct 04 '22 13:10

VhsPiceros