为什么这个代码编译没有错误,即使该类被标记为已过时?

Cad*_*oux 15 .net c# attributes obsolete visual-studio-2008

这是Visual Studio 2008.显然与扩展的静态类有关.

public class Dummy
{
    public readonly int x;

    public Dummy(int x)
    {
        this.x = x;
    }

    public override string ToString()
    {
        return x.ToString();
    }
}

[Obsolete("Do Not Use", true)]
public static class Extensions
{
    public static int Squared(this Dummy Dummy)
    {
        return Dummy.x * Dummy.x;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var d = new Dummy(42);
        Console.WriteLine(String.Format("{0}^2={1}", d, d.Squared()));
    }
}
Run Code Online (Sandbox Code Playgroud)

Eri*_*ert 9

在VS2010中也是如此.看起来像个bug.我会把它输入数据库.

您可以通过将属性放在实际方法上来解决错误.

谢谢你的报道!