Assembly.GetAssembly(Type) Método

Definição

Obtém o conjunto atualmente carregado onde o tipo especificado está definido.

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

Parâmetros

type
Type

Um objeto que representa um tipo na montagem que será devolvido.

Devoluções

O conjunto em que o tipo especificado está definido.

Exceções

type é null.

Exemplos

O exemplo seguinte recupera o assembly que contém o Int32 tipo e mostra o seu nome e localização do ficheiro.

using System;
using System.Reflection;

public class Example2
{
    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

Observações

Chamar este método é equivalente a recuperar o valor da Type.Assembly propriedade. No entanto, a Type.Assembly propriedade normalmente oferece um desempenho superior.

Para chamar este método, deve ter um Type objeto, o que significa que a assembly em que a classe está definida já deve estar carregada.

Aplica-se a