2012-08-09 118 views
0

我有一堆用C#編寫的dll,我知道它中使用的名稱空間名稱。我只需要獲取使用sqlconnection.open()方法或使用名稱空間system.data.sqlclient的dll列表。是否有一些方法可以通過編程方式使用C#來完成?在某些條件下過濾dll

+0

試試這個 - > http://stackoverflow.com/questions/949246/how-to-get-all-classes-within-namespace – bleeeah 2012-08-09 11:29:01

回答

1

試試這個:

class Program 
{ 
    static void Main(string[] args) 
    { 
     var methodsCallingDbConnectionOpen = AssemblyDefinition.ReadAssembly(typeof(Program).Assembly.Location) 
      .MainModule 
      .GetTypes() 
      .SelectMany(type => type.Methods) 
      .Where(method => method.HasBody && 
       method.Body.Instructions.Any(instruction => 
        instruction.OpCode.Code == Code.Callvirt && instruction.Operand is MethodReference && 
        ((MethodReference)instruction.Operand).FullName.Contains("System.Data.Common.DbConnection::Open"))); 

     foreach (var method in methodsCallingDbConnectionOpen) 
     { 
      Console.WriteLine(method); 
     } 

     Console.ReadLine(); 
    } 

    static void Foo() 
    { 
     using (var connection = new SqlConnection()) 
     { 
      connection.Open(); 
     } 
    } 
} 

產地:

System.Void ConsoleApplication1.Program ::富()

注:

  • 此示例使用Mono.Cecil
  • 此示例檢測到呼叫DbConnection.Open。爲了檢測正在使用哪個DbConnection的後代,你必須查看以前的IL指令(例如ctor調用)。
+0

感謝給的提示,現在將檢查 – GowthamanSS 2012-08-09 13:44:02

+0

我得到一個錯誤的\t'Mono.Cecil.AssemblyDefinition'不包含'ReadAssembly'的定義 – GowthamanSS 2012-08-09 14:49:22

+0

可以說我使用哪個版本的mono嗎 – GowthamanSS 2012-08-09 15:14:58