开发者

Marking native methods as deprecated/obsolete?

开发者 https://www.devze.com 2023-03-29 17:07 出处:网络
In .NET you can mark certain methods as obsolete so that developers are alerted when they attempt to use the deprecated method.

In .NET you can mark certain methods as obsolete so that developers are alerted when they attempt to use the deprecated method.

<Obsolete("Do not call this method.")> Private Sub FormerMethod()

The thing is you can only do this within classes you control. What do you do when you want your developers to avoid using certain methods on classes provided natively in .NET or by a vendor?

For example, what if you want your developer to prefer some custom extension method on DataTable rather than Select. I'd hate to have to define a custom version of the DataTable class if only to deprecate Select. That would leave us having to police whether or not the custom table was being used.

I'm aware that there are tools like (FxCop) that help enforce coding standards; however, my question is whether or not this can be handled without some third part开发者_开发知识库y tool.


While you may not like the answer, I would think wrapping would be the appropriate choice.

For a third party vendor's code, wrapping should be mandatory just to produce a level of abstraction for unit testing. And being a wrapper, you can obviously mark methods as Obsolete.

For .NET objects, it's a little more tricky. Obviously, someone can just include the namespace and go to town; I don't believe there is a 'code-only' solution to this problem like you want, other than perhaps making an integration test that searches over your solution and fails when that particular method you dont like is called.


If you want to mark a .Net Framework class as deprecated you can redefine it in one of the shared\infrastructure classes everyone is using.

Example:

namespace System.ComponentModel
{
    [Obsolete("Please do not use BackgroundWorker. Use class XXXX instead.")]    
    public class BackgroundWorker
    {

    }
}

It might be done also for specific methods by extending the original .Net Framework class, and then overriding specific methods that call the base method but are decorated with the Obsolete attribute.

However, you'll need resolve the circular class definition (maybe by using assembly reference alias).

0

精彩评论

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

关注公众号