FXCop构造函数参数的规则验证

本文关键字:规则 验证 参数 构造函数 FXCop | 更新日期: 2023-09-27 18:05:47

我正在尝试编写一个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构造函数,但无法找到传递给它作为方法名的第二个参数。

有谁能帮我一下吗?

FXCop构造函数参数的规则验证

谢谢你的帮助,虽然这不是确切的解决方案,但它带我到确切的一个。非常感谢!!

找到下面的代码,

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++;
            }

        }
    }
}