Procedura: determinare se un file è un assembly (C# e Visual Basic)
Un file è un assembly unicamente nei casi in cui è gestito e include nei metadati una voce assembly.Per ulteriori informazioni sugli assembly e sui metadati, vedere l'argomento Manifesto dell'assembly.
Procedura: determinare se un file è un assembly in modo manuale
Avviare Ildasm.exe (disassemblatore MSIL).
Caricare il file che si desidera testare.
Se ILDASM segnala che il file non è un file eseguibile portabile (PE, portable executable), tale file non è un assembly.Per ulteriori informazioni, vedere l'argomento Procedura: visualizzare il contenuto dell'assembly.
Procedura: determinare se un file è un assembly a livello di codice
Chiamare il metodo GetAssemblyName, passando il percorso completo e il nome del file che si desidera testare.
Se viene generata un'eccezione BadImageFormatException, il file non è un assembly.
Esempio
Nell'esempio riportato di seguito viene testata una DLL per verificare se è un assembly.
Module Module1
Sub Main()
Try
Dim testAssembly As Reflection.AssemblyName =
Reflection.AssemblyName.GetAssemblyName("C:\Windows\Microsoft.NET\Framework\v3.5\System.Net.dll")
Console.WriteLine("Yes, the file is an Assembly.")
Catch ex As System.IO.FileNotFoundException
Console.WriteLine("The file cannot be found.")
Catch ex As System.BadImageFormatException
Console.WriteLine("The file is not an Assembly.")
Catch ex As System.IO.FileLoadException
Console.WriteLine("The Assembly has already been loaded.")
End Try
Console.ReadLine()
End Sub
End Module
' Output (with .NET Framework 3.5 installed):
' Yes, the file is an Assembly.
class TestAssembly
{
static void Main()
{
try
{
System.Reflection.AssemblyName testAssembly =
System.Reflection.AssemblyName.GetAssemblyName(@"C:\Windows\Microsoft.NET\Framework\v3.5\System.Net.dll");
System.Console.WriteLine("Yes, the file is an assembly.");
}
catch (System.IO.FileNotFoundException)
{
System.Console.WriteLine("The file cannot be found.");
}
catch (System.BadImageFormatException)
{
System.Console.WriteLine("The file is not an assembly.");
}
catch (System.IO.FileLoadException)
{
System.Console.WriteLine("The assembly has already been loaded.");
}
}
}
/* Output (with .NET Framework 3.5 installed):
Yes, the file is an assembly.
*/
Il metodo GetAssemblyName carica il file di test, quindi lo rilascia dopo averne letto i dati.
Vedere anche
Riferimenti
Concetti
Assembly e Global Assembly Cache (C# e Visual Basic)