Deprecated Member Field with Property Attribute (Visual Studio Error)

I have a class with a member that I need to keep for stale code, and I need to mark it as stale so that the new code does not use it (without warning).

Let's say the class looks like this:

class MyClass { [Obsolete] private string _old = "..."; [Obsolete] public string Old { get { return _old; } } } 

I declare that the _old member _old is out of date to ensure that the new code inside the class does not use this field.

I also declare that the Old property is deprecated to ensure that code outside the class does not use the property.

When I compile this, I get a warning in the getter property that _old deprecated. I thought the compiler silently ignores this, since the property itself is deprecated.

Am I missing something or do I need to add #pragma warning disable/restore for deprecated member fields wherever they are used (although the method / property itself is marked as deprecated)?


The reason “I thought the compiler silently ignores this” is because it seems to be doing this for legacy classes:

 [Obsolete] public class MyObsoleteClass { public string DoSomething() { // No warning here, since the class itself is obsolete return new MyClass().Old; } } 

As @Heinzi replied: it looks like this is due to a bug in Visual Studio. I sent a connection report:

https://connect.microsoft.com/VisualStudio/feedback/details/1146809


It turns out that the error in Visual Studio is not limited to accessing an obsolete field from a property.

Access to an obsolete property from an obsolete method should not trigger a warning:

 public class Class2 { [Obsolete] public string Property { get; set; } [Obsolete] public void Method() { this.Property = "value"; // <-- Incorrect warning reported } } 

You should not do this from another class:

 public class Class3 { [Obsolete] public string Property { get; set; } } public class Class4 { [Obsolete] public string Method() { return new Class3().Property; // <-- Incorrect warning reported } } 

Interestingly, it works in the next class, and when you add this class, other warnings (from Class4 and Class2 ) will magically disappear.

 public class Class5 { [Obsolete] public void Method() { // No warning reported here, which is good. // This magically makes the other warnings disappear too! new Class2().Method(); } } 
+5
source share
2 answers

Your code is fine, and your understanding of how the Obsolete attribute should work is correct: if you look at the Output tab after compilation, you will notice that the compiler does not display a warning for your case (but it will display a warning if you remove the Obsolete attribute from your property, as expected).

You are correct, however, that Visual Studio sometimes displays a warning after making arbitrary changes to the code. This seems to be a bug in Visual Studio. If you can still play it with the latest version, I would suggest that you send an error report to http://connect.microsoft.com .

+2
source

That would be a smart feature, but I don't see any indication in the documentation that it should work that way. On the other hand, I would not use the Obsolate attribute in a private member (if the class is not extremely huge), but I would reorganize it. In your case, I would write the following:

 class MyClass { [Obsolete] public string Old { get; private set; } } 

And then you only need to change the _old settings to Old , and the problem will be solved.

0
source

Source: https://habr.com/ru/post/1214076/


All Articles