2012-05-03 4 views
0

사용자 지정 FxCop 규칙을 통해 특정 메서드 (MessageBox.Show)에 대한 호출을 거부하려고합니다. 나는 FxCop 규칙을 사용자 정의 구현 (XML 파일, BaseIntrospectionRule 등 상속) 얻는 방법을 알고있다. 여기에 내 질문은 내가 "확인"메서드에 넣은 것입니다.사용자 지정 규칙을 통해 FxCop에서 특정 메서드에 대한 호출을 검색하는 방법 - 무엇을 넣을 지 검사 메서드

다음은 웹에서 많은 것을 파고 들기에 기초한 초기 초안입니다. 그러나 실제로 표시된 두 필드에 무엇이 채워지는지 궁금합니다. ?????.

이 솔루션이 존재하더라도 제대로 작동하는지 확신 할 수 없습니다. MessageBox에 대한 모든 호출을 수신하고 있습니다. 원하는 것을하고 있는지 확인하기위한 어리석은 증거는 무엇입니까?

public override ProblemCollection Check(Member member) 
{ 
    Method method = member as Method; 
    if (method == null) 
    { 
     return null; 
    } 
    MetadataCollection<Instruction>.Enumerator enumerator = method.Instructions.GetEnumerator(); 
    while (enumerator.MoveNext()) 
    { 
     Instruction current = enumerator.Current; 
     switch (current.OpCode) 
     { 
      case OpCode.Call: 
      case OpCode.Callvirt: 
      { 
       Method method3 = current.Value as Method; 
       if (method3 == **?????**) 
       { 
        Problem item = new Problem(base.GetResolution(**?????**), current); 
        base.Problems.Add(item); 
       } 
       break; 
      } 
     } 
    } 
    return base.Problems; 
} 
+0

지침은 FxCop 규칙을 작성하는 '오래된 방법'입니다. 대신 방문 메소드를 사용하는 것이 더 쉽습니다. 나에 의해 작성된 사용자 정의 규칙은 string.ToString (다른 것들 중에서도) 호출을 조회합니다. https://fxcopcontrib.codeplex.com/SourceControl/changeset/view/7476#37972 – jessehouwing

+0

맞춤 규칙에 대한 최고의 자습서는 다음과 같습니다. 여기에 있습니다 : http://www.binarycoder.net/fxcop/index.html – jessehouwing

답변

1

당신은 내장 SpecifyMessageBoxOptions 규칙 반사판 같은 디 컴파일러를 사용하여 구축되는 방법을 살펴 할 수 있습니다. 가능한 다른 방법이 있지만 과도한 오 탐지 (false positive)를 초래할 것이라고 믿을만한 이유가없는 한 일반적으로 이름 비교가 좋습니다.

+0

와우, 정말 고마워. 나는 그것을 시도하고 다시보고 할 것입니다 ... –

+0

'C : \ Program Files (x86) \ Microsoft Visual Studio 11.0 \ Team Tools \ Static Analysis Tools \ FxCop \ Rules \' – Maslow

1

어때?

public override ProblemCollection Check(Member member) 
    { 
     Method method = member as Method; 
     if (method != null) 
     { 
      this.Visit(method.Body); 
     } 
     return this.Problems; 
    } 

    public override void VisitMethodCall(MethodCall call) 
    { 
     base.VisitMethodCall(call); 
     Method targetMethod = (Method)((MemberBinding)call.Callee).BoundMember; 
     if (targetMethod.Name.Name.Contains("MessageBox.Show")) 
      this.Problems.Add(new Problem(this.GetResolution(), call)); 
    }