2015-07-30 2 views
0

Я пытаюсь написать правило FXCop для проверки следующего вида кода,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 (используя (новый TraceGuard (DeclType, «StopWatchingForUpdates2»))) такие же или нет.

Я могу поймать Traceguard Constructor из своего правила FX cop, но не смог найти, какой второй параметр передан ему как имя метода.

Может ли кто-нибудь помочь мне в этом?

+0

Заканчивать http://fxcopcontrib.codeplex.com/SourceControl/latest#Dev/FxCopContrib/RegularExpressionDoesNotCompile.cs который проверяет Регулярное выражение строки передается в конструктор Regex. – jessehouwing

ответ

0

Благодарим за помощь, хотя это было не точное решение, но оно привело меня к точному. Большое спасибо!!

Найти мой код ниже,

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

      } 



     } 
    } 
} 
Смежные вопросы