Ottenere la lista delle dll .NET e degli assemblies referenziati dal progetto tramite la Reflection

Per ottenere una lista degli oggetti referenziati dal progetto è naturalmente necessario fare ricorso alla Reflection.
 
E' doveroso inoltre eliminare la possibilità di riferimenti circolari che causerebbero un loop infinito.
 
Io ho scelto di utilizzare una SortedList per verificare se l'elemento che si sta analizzando non sia già presente nella lista.
 
C#
 
Inserire:
using System.Reflection;
using System.Collections;
 
Definire inoltre tra gli oggetti sulla definizione delle variabili con visibilità "modulo"
 
SortedList slReference;
 
          void GeneraLista()
            {
                  slReference = new SortedList();
                  ReferencedAssembly(Assembly.GetExecutingAssembly());
                  textBox1.Text = "";
                  for ( int i = 0; i < slReference.Count; i++ ) 
                        textBox1.Text += String.Format("{0}\r\n", slReference.GetKey(i));
            }
           
            private void ReferencedAssembly(Assembly pAsm)
            {
                  foreach (AssemblyName asmName in pAsm.GetReferencedAssemblies())
                  {
                        Assembly refAssembly = Assembly.Load(asmName);
                        if (! CircularReference(refAssembly.CodeBase))
                              ReferencedAssembly(refAssembly);
                  }
            }
            private void ReferencedAssembly(string pName)
            {
                  bool retValue = false;
                  retValue = slReference.Contains(pName);
                  if (!retValue)
                        slReference.Add(pName,pName);
                  return retValue;
            }
     
VB.NET
 
Inserire tra gli imports:
Imports System.Reflection
Imports System.Collections
 
Definire inoltre la SortedList slReference in un'area di visibilità "modulo".
 
 
          Sub GeneraLista()
                  slReference = New SortedList
                  ReferencedAssembly(Assembly.GetExecutingAssembly)
                  textBox1.Text = ""
                  Dim i As Integer = 0
                  For i = 0 To slReference.Count - 1
                        textBox1.Text += String.Format("" & "{0}" & Environment.NewLine & "", slReference.GetKey(i))
                  Next 
            End Sub
 
            Private Sub ReferencedAssembly(ByVal pAsm As Assembly)
                  For Each asmName As AssemblyName In pAsm.GetReferencedAssemblies
                        Dim refAssembly As Assembly = Assembly.Load(asmName)
                        If Not CircularReference(refAssembly.CodeBase) Then
                              ReferencedAssembly(refAssembly)
                        End If
                  Next
            End Sub
 
            Private Function CircularReference(ByVal pName As String) As Boolean
                  Dim retValue As Boolean = False
                  retValue = slReference.Contains(pName)
                  If Not retValue Then
                        slReference.Add(pName, pName)
                  End If
                  Return retValue
            End Function

Print | posted on giovedì 15 febbraio 2007 23:30

Feedback

No comments posted yet.

Your comment:





 
Please add 7 and 6 and type the answer here:

Copyright © Luciano Bastianello

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski