简体   繁体   中英

How to Flag External Types/Methods as Deprecated/Obsolete in .NET?

We're trying to flag usage of external types (like ConfigurationManager ) at build time.

A custom code analysis dictionary could help [1] but only in scenarios where source is included in the project. Similarly the Obsolete attribute works on types included in the project.

I'm not even sure if Code Analysis rules are capable of inspecting method bodies? [2].

Any suggestions on how we can flag usage of external types/methods at build-time?

[1] - http://msdn.microsoft.com/en-us/library/bb514188.aspx

[2] - http://msdn.microsoft.com/en-us/library/dd172127(v=vs.90).aspx

One way to do that is to create a custom FxCop rule, checking fields during code analysis phase with something like this:

internal sealed class SampleRule : BaseIntrospectionRule
{

    public override ProblemCollection Check(Member member)
    {
        const string typeName = "System.DateTime";
        var field = member as Field;
        if (field == null || field.Type.FullName != typeName)
            return null;
        return new ProblemCollection 
        { 
            new Problem(new Resolution(field.Name.Name, "Type {0} is obsolete", typeName)) 
        };
    }
}

I'd recommend having a look at this tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM