开发者

Is it possible to mark an assembly as deprecated?

I'd like to produce a warning message when users compile code that references an assembly we're planning on removing. The contents of this assembly have been merged with another, and I'd like to encourage users to stop referencing the old assembly.

Unfortunately, it looks like ObsoleteAttribute is not valid for assemblies. Is there any other way to cause a compiler warning when a project referencing an assembly is built?

Thanks.

EDIT: For clarification, here's what the assemblies look like before and after the merge

Before the merge:

Assembly1:
namespace A.B.C {
    class C1
    ...
}

Assembly2:
namespace A.B.D {
    class D1
    ...
}

After the merge:

Assembly1:
(empty)

Assembly2:
namespace A.B.C {
    class C1
    ...
}

namespace A.B.D {
    class D1
    ...
}

Before the merge, users referenced both Assembly1 and Assembly2. After the merge, they only need to reference Assembly2, but I'd rather produce a warning that Assembly1 is no longer necessary than break their builds by removing Assembly1 ri开发者_JAVA百科ght away.

It sounds like I should use type forwarders to make sure that programs that have already been built against these assemblies continue to work without requiring recompilation, but I don't want to leave stub classes in Assembly1 just to flag the assembly as obsolete.


When moving types from one assembly to another the correct thing to do is to use a type forwarder, not to mark the old assembly classes as obsolete.

See: "CLR Hoser - The Wonders of Whidbey Factoring Features – Part I: Type Forwarders" by Richard Lander (2005)


Mark each of the classes in the old assembly as unusable.

This worked for me:

[Obsolete("Deprecated.  Use 'Substitute' class in 'NewAssembly' instead.")]
public class OutdatedClass : IDisposable, IXmlSerializable
{ 
    //code...
}

When attempting to compile, I got tons of warning about using that class.

You can make it a compile error to reference the assembly by passing True as the second parameter to the attribute.

[Obsolete("Deprecated.  Use 'Substitute' class in 'NewAssembly' instead.", true)]

public class OutdatedClass : IDisposable, IXmlSerializable
{ 
    //code...
}

I just tested that, and it also worked (i.e. - compiling failed)


I don't see anything specific on MSDN. You could, however, create your own assembly attribute but only if you think it is that important. Then again, doing so does not prevent someone from using it and I'm not sure how you could even let someone know that it has been deprecated.

Honestly, I think I would place [Obsolete] on the publicly accessible classes.

Based on what I read on @Eric's post and the link he provided along with WS' comment, use [TypeForwardedTo].

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜