Как добавить пользовательский набор правил в visual studio 15 с помощью анализатора Stylecop?

Я создал пользовательское правило в анализаторе stylecop.

Каждый метод должен иметь блок try catch.

вот код

[SourceAnalyzer(typeof(CsParser))]
    public class MyOwnCustomAnalyzer : SourceAnalyzer
    {
        public override void AnalyzeDocument(CodeDocument currentCodeDocument)
        {

                var codeDocument = (CsDocument)currentCodeDocument;
                if (codeDocument.RootElement != null && !codeDocument.RootElement.Generated)
                {
                    codeDocument.WalkDocument(new CodeWalkerElementVisitor<object>(this.InspectCurrentElement), null, null);
                }
        }

        private bool InspectCurrentElement(CsElement element, CsElement parentElement, object context)
        {

                if (element.ElementType == ElementType.Method)
                {
                    bool boolIsTryFound = false;
                    bool boolIsCatchFound = false;
                    var tempDocument = (CsDocument)element.Document;
                    var objReader = tempDocument.SourceCode.Read();
                    string strCode;
                    while ((strCode = objReader.ReadLine()) != null)
                    {
                        if (strCode.Contains("try"))
                        {
                            boolIsTryFound = true;
                        }

                        if (boolIsTryFound)
                        {
                            if (strCode.Contains("catch"))
                            {
                                boolIsCatchFound = true;
                            }
                        }
                    }

                    if (boolIsTryFound && (boolIsCatchFound == false))
                    {
                        AddViolation(parentElement, "MyOwnCustomRule", "CatchShouldBeImplemented");

                    }
                }

            return true;
        }
    }

Я перешел по следующей ссылке, чтобы создать собственное правило и скопировать Dll в папку расширения.

Пользовательское правило StyleCop

он отображается в окне настроек Stylecop, но проверяет метод без использования метода catch.

0 ответов

Другие вопросы по тегам