views:

232

answers:

2

I would like to apply ObsoleteAttribute to a property, but it seems that compiler generates warnings/errors only for direct usage of attribute, any indirect usage is silently ignored.

I think the following example illustrates the problem very well:

using System;
class Program
{
 static void Main(string[] args)
 {
  var o = new Old();
  Console.WriteLine(o.Geezer); // compilation fails: 'ObsoleteAttributeTest.Program.Old.Geezer' is obsolete: 'Some error' 
  Console.WriteLine(o.Geezer.Attributes); // compiles OK
 }

 class Old
 {
  [ObsoleteAttribute("Some error", true)]
  public System.Xml.XmlElement Geezer { get { return null; } }
 }
}
+1  A: 

I believe this is a known bug in the latest C# compiler (C# 3.0). There is another question on StackOverflow relating to this problem.

Jeff Yates
A: 

var o = new Old(); throws error. error CS0246: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?) If we change this as Old o=new Old(); still the second line throws error like error CS0619: 'Program.Old.Geezer' is obsolete: 'Some error' . I am getting whats going here..!!!!!!!!!!

I should have stated that I'm using latest C# compiler version. You're using 2.0 or even something older, check this message (linked above too, btw) http://stackoverflow.com/questions/577132/why-are-c-collection-properties-not-flagged-as-obsolete-when-calling-properties/577152#577152
dolzenko