Domanda

I would like to run a command to see all rules that FxCop will support (based on the DLLs in its Rules directory).

Is this possible?

È stato utile?

Soluzione

The rule list can be viewed in the Rules tab of the FxCop UI (e.g.: http://www.binarycoder.net/fxcop/html/screenshot.png).

If you want a text version and don't have a screen clipping tool that will allow you to extract this from the UI, the rules list is pretty trivial to extract with a bit of reflection. You can either invoke internal types and methods in the FxCop object model to do this, or you can look for concrete classes that implement the Microsoft.FxCop.Sdk.IRule interface in the rule assemblies. e.g.:

internal static class Program
{
    private static void Main(string[] args)
    {
        Program.EnumerateFxCopRules(@"C:\Program Files (x86)\Microsoft Fxcop 10.0\Rules");
        Console.ReadLine();
    }

    private static void EnumerateFxCopRules(string ruleDirectoryPath)
    {
        foreach (var ruleAssembly in Program.GetAssemblies(ruleDirectoryPath))
        {
            Program.WriteRuleList(ruleAssembly);
        }
    }

    private static IEnumerable<Assembly> GetAssemblies(string directoryPath)
    {
        var result = new List<Assembly>();
        foreach (var filePath in Directory.GetFiles(directoryPath, "*.dll"))
        {
            try
            {
                result.Add(Assembly.LoadFrom(filePath));
            }
            catch (FileLoadException)
            {
                Console.WriteLine("FileLoadException attempting to load {0}.", filePath);
            }
            catch (BadImageFormatException)
            {
                Console.WriteLine("BadImageFormatException attempting to load {0}.", filePath);
            }
        }

        return result;
    }

    private static void WriteRuleList(Assembly ruleAssembly)
    {
        Console.WriteLine(ruleAssembly.Location);

        foreach (var ruleType in ruleAssembly.GetTypes().Where(t => (!t.IsAbstract) && typeof(IRule).IsAssignableFrom(t)))
        {
            Console.WriteLine("\t{0}", ruleType.FullName);
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top