2010-01-14 2 views
3

인터페이스, 추상 클래스, 열거 형, 구조체 등 주어진 유형이 종속되어있는 모든 유형을 찾으려고합니다. 어셈블리를로드하고 그 안에 정의 된 모든 유형의 목록을 인쇄하고 싶습니다. , 그리고 그들의 의존성.CLR 기반 언어 어셈블리에서 특정 유형의 모든 유형 의존성을 찾는 방법은 무엇입니까?

지금까지 저는 CLR 어셈블리가 Mono.Cecil을 사용하여 의존하는 모든 외부 유형을 찾을 수있었습니다.

using System; 
using Mono.Cecil; 
using System.IO; 

FileInfo f = new FileInfo("SomeAssembly.dll"); 
AssemblyDefinition assemDef = AssemblyFactory.GetAssembly (f.FullName); 
List<TypeReference> trList = new List<TypeReference>(); 

foreach(TypeReference tr in assemblyDef.MainModule.TypeReferences){ 
    trList.Add(tr.FullName); 
} 

이 목록은 또한 모노 disasembler, 예를 들어 "monodis SomeAssembly.dll --typeref"를 사용하여 얻을 수 있지만 것 나던이 목록은 기본 요소, 예를 들어 System.Void, 선택 System.Int32 등

을 포함하는

형식이 동일한 어셈블리에 정의되어 있어도 각 형식을 개별적으로 처리하고 주어진 형식이 종속 된 모든 형식을 가져와야합니다. Mono.Cecil 또는 다른 프로젝트를 사용하여이 작업을 수행 할 수있는 방법이 있습니까?

어셈블리를로드 한 다음 각 정의 된 형식을 반복하고 형식의 일리로드를로드하고 참조를 검색하여 수행 할 수 있음을 알고 있지만 더 좋은 방법이 있다고 확신합니다. 이상적으로는 익명의 내부 클래스에서도 작동합니다.

동일한 어셈블리에 여러 모듈이 정의되어있는 경우에도 작동해야합니다.

답변

1

AJ -Oisin

, 나는 내가 어셈블리의 형식을 통과하는 데 필요한 같은 문제가 있었다 나는 Mono.Cecil 사용에 정착했다. 방법은 내가 각 클래스를 통해 산책 할 수 있었고 클래스의 속성이 CLR 유형 대신 다른 클래스가 아닌 경우 재귀 함수를 사용했습니다.

private void BuildTree(ModuleDefinition tempModuleDef , TypeDefinition tempTypeDef, TreeNode rootNode = null) 
    { 
      AssemblyTypeList.Add(tempTypeDef); 

      TreeNode tvTop = new TreeNode(tempTypeDef.Name); 

      // list all properties 
      foreach (PropertyDefinition tempPropertyDef in tempTypeDef.Properties) 
      { 
       //Check if the Property Type is actually a POCO in the same Assembly 
       if (tempModuleDef.Types.Any(q => q.FullName == tempPropertyDef.PropertyType.FullName)) 
       { 
        TypeDefinition theType = tempModuleDef.Types.Where(q => q.FullName == tempPropertyDef.PropertyType.FullName) 
                   .FirstOrDefault(); 
        //Recursive Call 
        BuildTree(tempModuleDef, theType, tvTop); 

       } 

       TreeNode tvProperty = new TreeNode(tempPropertyDef.Name); 
       tvTop.Nodes.Add(tvProperty); 
      } 

      if (rootNode == null) 
       tvObjects.Nodes.Add(tvTop); 
      else 
       rootNode.Nodes.Add(tvTop); 

    } 

이 기능은 내 주요 기능으로 요점라고있는

 public void Main() 
     { 
     AssemblyDefinition assemblyDef = AssemblyDefinition.ReadAssembly(dllname); 

     //Populate Tree 
     foreach (ModuleDefinition tempModuleDef in assemblyDef.Modules) 
     { 
      foreach (TypeDefinition tempTypeDef in tempModuleDef.Types) 
      { 
       BuildTree(tempModuleDef ,tempTypeDef, null); 
      } 
     } 

     } 
1

NDepend를 살펴보십시오.

+0

감사 Oisin, 내가 NDepend에 대해 알고 는, 그것은 좋은 제품입니다. 종속 형식 목록을 생성하여 다른 도구에 제공 할 수 있습니다. 그러므로 NDepend는 내가 필요한 도구가 아닙니다. –

관련 문제