2009-04-15 1 views
17

즉, 컴파일되지 않은 어셈블리를 만들 수 있습니다 (검사 코드가 제거되지 않은 것으로 가정). 클래스에는 사용자 지정 특성 (예 : 작성자 및 버전)이 없습니다 ("있어야 함")?컴파일 타임 (실행 시간 아님) 동안 C#에서 사용자 지정 특성을 쿼리 할 수 ​​있습니까

using System; 
using System.Reflection; 
using System.Collections.Generic; 


namespace ForceMetaAttributes 
{ 

    [System.AttributeUsage (System.AttributeTargets.Method, AllowMultiple = true)] 
    class TodoAttribute : System.Attribute 
    { 
     public TodoAttribute (string message) 
     { 
      Message = message; 
     } 
     public readonly string Message; 

    } 

    [System.AttributeUsage (System.AttributeTargets.Class | 
     System.AttributeTargets.Struct, AllowMultiple = true)] 
    public class AttributeClass : System.Attribute 
    { 
     public string Description { get; set; } 
     public string MusHaveVersion { get; set; } 


     public AttributeClass (string description, string mustHaveVersion) 
     { 
      Description = description; 
      MusHaveVersion = mustHaveVersion ; 
     } 

    } //eof class 


    [AttributeClass("AuthorName" , "1.0.0")] 
    class ClassToDescribe 
    { 
     [Todo (" A todo message ")] 
     static void Method() 
     { } 
    } //eof class 

    //how to get this one to fail on compile 
    class AnotherClassToDescribe 
    { 

    } //eof class 

class QueryApp 
{ 
     public static void Main() 
     { 

       Type type = typeof(ClassToDescribe); 
       AttributeClass objAttributeClass; 


       //Querying Class Attributes 

       foreach (Attribute attr in type.GetCustomAttributes(true)) 
       { 
         objAttributeClass = attr as AttributeClass; 
         if (null != objAttributeClass) 
         { 
           Console.WriteLine("Description of AnyClass:\n{0}", 
                    objAttributeClass.Description); 
         } 
       } 



       //Querying Class-Method Attributes 

       foreach(MethodInfo method in type.GetMethods()) 
       { 
         foreach (Attribute attr in method.GetCustomAttributes(true)) 
         { 
           objAttributeClass = attr as AttributeClass; 
           if (null != objAttributeClass) 
           { 
             Console.WriteLine("Description of {0}:\n{1}", 
                      method.Name, 
                      objAttributeClass.Description); 
           } 
         } 
       } 
       //Querying Class-Field (only public) Attributes 

       foreach(FieldInfo field in type.GetFields()) 
       { 
         foreach (Attribute attr in field.GetCustomAttributes(true)) 
         { 
           objAttributeClass= attr as AttributeClass; 
           if (null != objAttributeClass) 
           { 
             Console.WriteLine("Description of {0}:\n{1}", 
                      field.Name,objAttributeClass.Description); 
           } 
         } 
       } 
       Console.WriteLine ("hit Enter to exit "); 
       Console.ReadLine(); 
     } //eof Main 
} //eof class 

} //eof namespace 


//uncomment to check whether it works with external namespace 
//namespace TestNamespace { 

// class Class1 { } 
// class Class2 { } 

//} 

편집 : 여기

내가 실행 시간 동안 질의에 사용한 코드입니다 그냥 답변에 대한 내 선택을 정당화합니다. 나는 casperOne이 질문의 정답을 제공했다고 생각합니다.

그러나 질문하는 이유는 weak 인 것 같습니다. 아마 내가 같은 일부 외부 도구를 사용하기 시작해야합니다

편집 ... PEX, NUnit과 또는 다른 단위 테스트 프레임 워크를 사용하여, FinalBuilder 또는이 "요구 사항"검사 단위 테스트를 만들 나는 작은 추가 code snippet 콘솔 프로그램의 체크를 수행하는 답변의 끝에 ... 논평, 비판 또는 개선 제안
다시 한번이 "요구 사항"이 단위 테스트의 일환으로 구현되어야한다는 것을 깨달았습니다. "체크인"

답변

7

아니요, t에 연결할 수 없습니다. 그는 집회를 집계하고 그것이 있는지 확인한다.

그러나 컴파일러를 실행하는 것 이상으로 구성된 빌드 프로세스에 연결할 수 있습니다. 빌드 후 리플렉션을 통해 어셈블리를 확인한 다음 필수 특성이없는 경우 빌드를 실패하는 사용자 지정 MSBUILD 작업 (또는 사용중인 경우 NAnt)을 만들 수 있습니다.

당연히 여전히이어야합니다. 코드에서도이를 확인하십시오. 당신이하려고하는 것은 적절한 런타임 검사를 대신하는 좋은 방법이 아닙니다.

+0

Thaks! "당신이하려고하는 것은 적절한 런타임 검사 대신 좋은 대안이 아닙니다."라는 말은 무엇을 의미합니까? –

+0

@YordanGeorgiev : 속성이 적용되었는지 확인하는 빌드 프로세스가 있더라도 런타임 코드를 계속 확인하여 속성이 적용되었는지 확인해야합니다. 컴파일 시간에 잡은 것 같아서 거기서 체크하는 것을 멈출 수 없습니다. – casperOne

+0

따라서 최소한 4 개의 "필수 항목"속성을 가져야하는 요구 사항은 반사 때문에 성능에 영향을 미칩니다 ... 유닛 테스트에서 점검을 수행하는 것이 더 좋은 아이디어 일 것 같네요! –

1

속성은 런타임에만 있습니다. 그러나 :

FXCop (정적 분석)에서 속성이 정의되지 않은 경우 실패하고 빌드/체크 인 프로세스가 해당 규칙을 검사하여 적절히 실패 할 수있는 규칙을 만들 수 있습니다.

+0

100 % 사실이 아닙니다. "폐기 됨"과 "조건부"는 모두 컴파일 타임 속성입니다. –

1

C# 컴파일 프로세스에 연결하는 방법을 모르지만 다른 방법을 사용하여 포스트 빌드 이벤트에서 시작된 사용자 지정 도구를 만들어 어셈블리를로드하고 반영 할 수 있습니다. 도구가 반환하는 결과에 따라 전체 빌드 프로세스가 성공 또는 실패로 이어 지므로 도구에 오류가 발생하고 빌드가 실패하게되고 콘솔에 쓰는 오류에 대한 자세한 내용을 제공 할 수 있습니다.

+0

브라이언 (Brian), 감사합니다. –

4

원하는 작업을 수행하기 위해 DLL에 반영된 빌드 후 단계를 실행할 수 있습니다.

DLL을로드하고 형식을 반영하는 명령 줄 응용 프로그램을 작성해야합니다. 그런 다음 명령 줄 앱을 빌드 후 단계로 실행합니다. 나는 이것을 과거에 해왔다. 리플렉션 API를 이해한다고 가정 할 때 몹시 어렵지 않습니다.

PostSharp aspect 지향 프로그래밍을 수행하려면이 작업을 수행하십시오. 실제로 꽤 멋지다.

+0

이 도구의 작은 초안을 추가했습니다. 내일 볼 수있는 유망한 것으로 보입니다 ... 핀란드에서는 10시 43 분입니다. 의미있는 것을 찾아 내면 코드를 게시 할 것입니다 ... –

+0

Arggh ...이 솔루션이 닫히면 냄새가납니다. –

1

제게 이것은 컴파일 문제보다 테스트 문제와 비슷합니다. 즉, "내 코드가 올바르게 작성되었는지 어떻게 알 수 있습니까?"라고 묻는 것입니다. "정확하게 쓰여진"곳에는 (모든 것들 중에서) 모든 클래스가 특정 속성으로 꾸며진다는 의미가 있습니다. 나는 여러분의 속성 포함 규칙이 실제로 따라 갔는지 확인하는 단위 테스트를 작성하는 것을 고려할 것입니다. 성공적인 빌드 (체크 인)의 조건으로 빌드 (체크인 전) 후에 빌드 (및/또는 체크인) 프로세스에서이 특정 테스트 세트를 실행할 수 있습니다. 테스트를 실행하기 위해 완료해야하기 때문에 컴파일을 중단하지는 않지만 빌드를 손상시킬 수 있습니다.

+0

단위 테스트가 항상 전체 빌드에 100 % 적합하지는 않습니다. 단위계 테스트에서 어느 정도는 부정 행위를하는 것을 보았습니다 ... 전체적인 생각이 컴파일되지 않고 강제 사용자 지정 특성이 최소 인 경우 (예 : 작성자, 버전) –

+0

나는 아마도 속성 당 단일 단위 테스트. 어셈블리의 모든 클래스 (또는 클래스의 메서드)를 거쳐 특성의 존재를 확인합니다. 각 클래스/메소드에 대해 별도의 테스트를 작성할 필요는 없습니다. – tvanfosson

+0

답변 해 주셔서 감사합니다 tvanfosson! 이 검사는 단위 테스트 과정의 일부로 수행되어야 함을 지적하십시오. 예를 들어 어떻게 수행 할 수 있는지 예제 코드 조각을 추가했습니다 (그러나 NUnit을 사용할 수 있으므로 특정 단위 테스트의 일부가 아닙니다) , PEX 또는 다른 무엇이든지 그리고 나는 모든 변이를 ​​모른다. 당신의 대답은 항상 큰 그림을 먼저보고 나중에 코딩을 시작해야한다는 것을 상기시켰다. –

0
//PLEASE COMMENT IF YOU FIND BUGS OR SUGGEST IMPROVEMENTS 


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Reflection; 

namespace MustHaveAttributes 
{ 
[AttributeClass ("Yordan Georgiev", "1.0.0")] 
class Program 
{ 


static void Main (string [] args) 
{ 
    bool flagFoundCustomAttrOfTypeAttributeClass = false; 
    Console.WriteLine (" START "); 

    // what is in the assembly 
    Assembly a = Assembly.Load ("MustHaveAttributes"); 
    Type[] types = a.GetTypes(); 
    foreach (Type t in types) 
    { 
    object[] arrCustomAttributes = t.GetCustomAttributes (true); 


    if (arrCustomAttributes == null || arrCustomAttributes.GetLength (0) == 0) 
    { 
    //DO NOT CHECK IN 
    ExitProgram (t, "Found class without CustomAttributes"); 
    } 


    foreach (object objCustomAttribute in arrCustomAttributes) 
    { 
    Console.WriteLine ("CustomAttribute for type is {0}", t); 
    if (objCustomAttribute is AttributeClass) 
    flagFoundCustomAttrOfTypeAttributeClass = true; 
    } 

    if (flagFoundCustomAttrOfTypeAttributeClass == false) 
    { //DO NOT CHECK IN 
    ExitProgram (t, "Did not found custom attribute of type AttributeClass"); 
    } 
    Console.WriteLine ("Type is {0}", t); 
    } 
    Console.WriteLine ("{0} types found", types.Length); 

    //NOW REQUIREMENTS IS PASSED CHECK IN 
    Console.WriteLine (" HIT A KEY TO EXIT "); 
    Console.ReadLine(); 
    Console.WriteLine (" END "); 
} 



static void ExitProgram (Type t, string strExitMsg ) 
{ 

    Console.WriteLine (strExitMsg); 
    Console.WriteLine ("Type is {0}", t); 
    Console.WriteLine (" HIT A KEY TO EXIT "); 
    Console.ReadLine(); 

    System.Environment.Exit (1); 

} 
} //eof Program 


//This will fail even to compile since the constructor requires two params 
//[AttributeClass("OnlyAuthor")] 
//class ClassOne 
//{ 

//} //eof class 


////this will not check in since this class does not have required custom 
////attribute 
//class ClassWithoutAttrbute 
//{ } 



[AttributeClass("another author name " , "another version")] 
class ClassTwo 
{ 

} //eof class 


[System.AttributeUsage (System.AttributeTargets.Class | 
System.AttributeTargets.Struct, AllowMultiple = true)] 
public class AttributeClass : System.Attribute 
{ 

public string MustHaveDescription { get; set; } 
public string MusHaveVersion { get; set; } 


public AttributeClass (string mustHaveDescription, string mustHaveVersion) 
{ 
    MustHaveDescription = mustHaveDescription; 
    MusHaveVersion = mustHaveVersion; 
} 

} //eof class 

} // EOF 네임 스페이스 응답에 대한

관련 문제