Wednesday, February 9, 2011

Crick In Neck A Virus

VB.NET: retrieve the assemblies referenced in our application

La classe Assembly prevede il metodo GetReferencedAssembly() per recuperare l’elenco degli AssemblyName ralativi agli assembly referenziati da un determinato assembly.

The problem is that if we have a structure with more dll referenced in the chain, the method returns only the first level of the same chain.

Suppose we have the following structure:

image

or the project AssemblyExtension reference the dll AssemblyExtensionLIB1 which, in turn, reference the AssemblyExtensionLIB2.

In this case, the method GetReferencedAssembly (), applied to an assembly AssemblyExtension, returns the following list:

SNAGHTML4498867

As you can see there is no trace of AssemblyExtensionLIB2. The following method

di estensione provvede ad eseguire un algoritmo ricorsivo per recuperare una lista di AssemblyName contenente tutte le reference dell’assembly:

  1. Imports System.Runtime.CompilerServices
  2. Imports System.Reflection
  3.  
  4. Module AssemblyExtension
  5.  
  6.     < Extension ()> _
  7.      Public Sub GetAllReferenceAssemblies( ByVal sourceAssembly As Assembly ,
  8.                                           ByVal allowGacAssembly As Boolean ,
  9.                                           ByVal list As ICollection ( Of AssemblyName ))
  10.          If sourceAssembly Is Nothing Then Throw New ArgumentNullException ( "Source Assembly" )
  11.          If list Is Nothing Then Throw New ArgumentNullException ( "Destination List" )
  12.         list.Add(sourceAssembly.GetName())
  13.          If allowGacAssembly OrElse Not sourceAssembly.GlobalAssemblyCache Then
  14.              Dim assemblies = sourceAssembly.GetReferencedAssemblies()
  15.              For Each assemblyName In assemblies
  16.                  Dim query = From a In list _
  17.                              Where a.FullName = assemblyName.FullName _
  18.                              Select a
  19.                  If query.Count = 0 Then
  20.                      Dim assembly As Assembly = assembly.Load(assemblyName.FullName)
  21.                     assembly.GetAllReferenceAssemblies(allowGacAssembly, list)
  22. End If Next
  23. End If End
  24. Sub End Module

The extension method is applicable on any assemble and the parameters are:

  • allowGacAssembly: indicates whether to search the references in DLLs contained in the GAC;
  • list: the list that will contain AssemblyName search result.

If your project before writing:

  1. Dim list = New List (Of AssemblyName )
  2. Assembly. GetEntryAssembly (). GetAllReferenceAssemblies ( True , list)

We get:

SNAGHTML46ed88a

allowGacAssembly While if we set = false:

SNAGHTML46fb735

Attenzione: il metodo GetReferencedAssembly restituisce gli assembly effettivamente referenziati (cioè quelli per i quali c’è almeno un tipo utilizzato) e non quelli inseriti nelle referenze di Visual Studio.

 

0 comments:

Post a Comment