Assembly.GetAssembly(Type) Metodo

Definizione

Ottiene l'assembly attualmente caricato in cui è definito il tipo specificato.

public:
 static System::Reflection::Assembly ^ GetAssembly(Type ^ type);
public static System.Reflection.Assembly? GetAssembly (Type type);
public static System.Reflection.Assembly GetAssembly (Type type);
static member GetAssembly : Type -> System.Reflection.Assembly
Public Shared Function GetAssembly (type As Type) As Assembly

Parametri

type
Type

Oggetto che rappresenta un tipo nell'assembly che verrà restituito.

Restituisce

Assembly

Assembly in cui è definito il tipo specificato.

Eccezioni

type è null.

Esempio

Nell'esempio seguente viene recuperato l'assembly che contiene il Int32 tipo e ne vengono visualizzati il nome e il percorso del file.

using namespace System;
using namespace System::Reflection;

void main()
{
   // Get a Type object.
   Type^ t = int::typeid;
   // Instantiate an Assembly class to the assembly housing the Integer type.
   Assembly^ assem = Assembly::GetAssembly(t);
   // Display the name of the assembly.
   Console::WriteLine("Name: {0}", assem->FullName);
   // Get the location of the assembly using the file: protocol.
   Console::WriteLine("CodeBase: {0}", assem->CodeBase);
}
// The example displays output like the following:
//    Name: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
using System;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      // Get a Type object.
      Type t = typeof(int);
      // Instantiate an Assembly class to the assembly housing the Integer type.
      Assembly assem = Assembly.GetAssembly(t);
      // Display the name of the assembly.
      Console.WriteLine("Name: {0}", assem.FullName);
      // Get the location of the assembly using the file: protocol.
      Console.WriteLine("CodeBase: {0}", assem.CodeBase);
   }
}
// The example displays output like the following:
//    Name: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
Imports System.Reflection

Module Example
   Public Sub Main()
      ' Get a Type object.
      Dim t As Type = GetType(Integer)
      ' Instantiate an Assembly class to the assembly housing the Integer type.
      Dim assem As Assembly = Assembly.GetAssembly(t)
      ' Display the name of the assembly.
      Console.WriteLine("Name: {0}", assem.FullName)
      ' Get the location of the assembly using the file: protocol.
      Console.WriteLine("CodeBase: {0}", assem.CodeBase)
   End Sub
End Module
' The example displays output like the following:
'    Name: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
'    CodeBase: file:'/C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll

Commenti

Chiamare questo metodo equivale a recuperare il valore della Type.Assembly proprietà . Tuttavia, la Type.Assembly proprietà offre in genere prestazioni superiori.

Per chiamare questo metodo, è necessario disporre di un oggetto , ovvero l'assembly in cui è definita la classe deve Type essere già caricato.

Si applica a