开发者

Is Obsolete attribute only checked at Compile time?

I wonder that the obsolete attribute is checked at only runtime?

Think that you have two assemblies. Assembly A uses a method from Assembly B. After that we mark the method in Assembly B as obsolete which causes a compile time error when compiling assembly A.

No problem so far but the question is whether the older assembly A continue to work with new Assembly 开发者_JS百科B or not? Thanks


It depends on what you are doing. The [Obsolete] attribute is primarily for use at compile-time, but be aware that some parts of the runtime have different behavior when it is present (see below). It might cause problems, even to existing code that is not rebuilt, so we must conclude that NO, [Obsolete] is not checked only at compile time.

For example, the code below will write Foo but not Bar:

using System;
using System.Xml.Serialization;
public class Data
{
    public int Foo { get; set; }
    [Obsolete] public int Bar {get;set;}

    static void Main()
    {
        var data = new Data { Foo = 1, Bar = 2 };
        new XmlSerializer(data.GetType()).Serialize(Console.Out, data);
    }
}

(XmlSerializer is a runtime too - not part of the compiler)


Building an assembly that uses a method from another assembly which is marked as Obsolete causes a compile time warning (Unless you have 'display warnings as errors' enabled).

There is nothing stopping you using this method while ever it remains in the referenced assembly. The Obsolete attribute is there as a way for library developers to let the people who use the library know that they should be looking to use a different method to achieve what they need.

To answer your question, yes, an older assembly A will continue to work with a new assembly B. (providing the assembly version remains the same)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜