Assembly.GetAssembly(Type) 方法

定义

获取在其中定义指定类型的当前加载的程序集。

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

参数

type
Type

一个对象,该对象表示将返回的程序集中的类型。

返回

在其中定义指定类型的程序集。

例外

typenull

示例

以下示例检索包含 Int32 类型的程序集,并显示其名称和文件位置。

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 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

注解

调用此方法等效于检索 属性的值 Type.Assembly 。 但是, Type.Assembly 属性通常提供卓越的性能。

若要调用此方法,必须具有 对象 Type ,这意味着必须已加载在其中定义类的程序集。

适用于