繁体   English   中英

构造器参数的FXCop规则验证

[英]FXCop Rule validation for Constructor Parameters

我正在尝试编写FXCop规则来验证以下类型的代码,

namespace ClassTarget
{
    public class Class1
    {
        private static readonly Type DeclType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
        public void StartWatchingForUpdates1()
        {
            using (new TraceGuard(DeclType,"StartWatchingForUpdates1"))
            {
                Console.Write("Test");
            }
        }

        /// <see cref="IAutomaticUpdaterBackendHelper.StopWatchingForUpdates"/>
        public void StopWatchingForUpdates2()
        {
            using (new TraceGuard(DeclType, "StopWatchingForUpdates2"))
            {

            }
        }

    }
}

在这里,我必须验证方法名称和TraceGuard构造函数内部调用的字符串(使用(new TraceGuard(DeclType,“ StopWatchingForUpdates2”)))是否相同。

我能够从FX cop规则中捕获Traceguard构造函数,但无法找到作为方法名称传递给它的第二个参数是什么。

谁可以帮我这个事?

谢谢您的帮助,尽管它不是确切的解决方案,但它使我找到了确切的解决方案。 非常感谢!!

在下面找到我的代码,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.FxCop;
using Microsoft.FxCop.Sdk;


namespace MyCustomRule
{
    public class MyCustomRule : BaseIntrospectionRule
    {
        private TypeNode m_ArgumentException;
        string errorMessage, methodName;
        Boolean problemyn;
        public MyCustomRule() :
            base("MyCustomRule", "MyCustomRule.Connection", typeof(MyCustomRule).Assembly)
        {
        }

        public override ProblemCollection Check(Member member)
        {
            Method method = member as Method;

            MetadataCollection<Instruction> enumerator = method.Instructions;
            methodName = method.Name.ToString();

            StatementCollection stmt = method.Body.Statements;
            try
            {
                problemyn = false;
                VisitStatements(stmt);

                if (problemyn)
                {
                    Resolution resolu = GetResolution(new string[] { method.ToString() + errorMessage });
                    Problems.Add(new Problem(resolu));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return Problems;
        }

        public override void VisitExpression(Expression expr)
        {

            Construct cnstruct;
            InstanceInitializer instInit;

            int i = 0;

            cnstruct = expr as Construct;
              if (cnstruct == null)
                return;

            instInit = (InstanceInitializer)((MemberBinding)cnstruct.Constructor).BoundMember;

            foreach (Expression operand in cnstruct.Operands)
            {
                if (instInit.Parameters[i].Name.Name == "strMethodName")
                {
                    Literal lit;
                    String litString;

                    lit = operand as Literal;
                    if (lit == null)
                        continue;

                    litString = lit.Value as String;
                    if (litString == null)
                        continue;

                    if (methodName == litString )
                    {
                        break;
                    }
                    else
                    {
                        problemyn = true;
                        errorMessage += methodName + " " + litString;
                    }


                }
                i++;

            }



        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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