Ignore ObsoleteAttribute Compiler Error

I have an enumeration value marked with the following attribute. The second parameter tells the compiler an error whenever a value is used. I want this behavior for everyone who implements my library, but I need to use this enumeration value in my library. How to tell the compiler to ignore the Obsolete error for several uses in my library.

public enum Choices { One, Two, [ObsoleteAttribute("don't use me", true)] Three, Four } 

Solution (thanks everyone)

 public class EnumHack { static EnumHack() { // Safety check if (Choices!= (Choices)Enum.Parse(typeof(Choices), "Three")) throw new Exception("Choices.Three != 3; Who changed my Enum!"); } [Obsolete("Backwards compatible Choices.Three", false)] public const Choices ChoicesThree = (Choices)3; } 
+3
source share
5 answers

Assign a separate constant somewhere like this:

 private const Choices BackwardsCompatibleThree = (Choices) 3; 

Note that anyone else can do the same.

+2
source

How about using #pragma to disable warnings around specfic code?

 #pragma warning disable 0612 // Call obsolete type/enum member here #pragma warning restore 0612 

Note to visitors, this only works with types and enumerated members. As far as I know, this will not work with other type members (e.g. methods, properties, etc.).

+2
source

What I see is that you use this public enumeration for private logic, and after reinstalling it, you still need this logic inside.

I see 2 options:

  • Map it to a private Enum when you use it for branching logic. You should be able to translate directly from one to another.
  • Pass it from int, so never use the actual value of Enum in your code.

As John points out above, anyone using your library might also WILL (I know where you work), just hack it anyway.

+1
source

This may not be the finest solution in the world, but you can try to trick the compiler by assigning values ​​to an enumeration, and then starting internal calls. For example, this application starts:

 namespace ConsoleApplication { class Program { static void Main(string[] args) { TestMethod((Choices)3); } private static int TestMethod(Choices choice) { return 1; } } public enum Choices { One = 1, Two = 2, [ObsoleteAttribute("don't use me", true)] Three = 3, Four = 4 } } 

I thought Enum.Parse would work, but it gets an error at runtime, so don't do this:

 (Choices)Enum.Parse(typeof(Choices), "Choices.Three") 

I have no experience with legacy enumerations, so I would recommend a bit of good testing around this.

+1
source

TheSoftwareJedi correctly points out that this will not work with an obsolete attribute set as an error. The following β€œresponse” only works if an outdated notification is called as a warning.


From Visual Studio, you can do this based on each project:

  • Go to the Project Properties page for the project that you want to disable with an outdated warning.
  • Click the Build tab: Errors and Warnings: Disable Warnings
  • Enter in this case the warning number, 0612.

Other projects will continue to receive an outdated warning, but this project will not. Please note that this will disable obsolete ALL warnings.

0
source

All Articles