Assembly.GetAssembly(Type) 方法

定义

获取在其中定义指定类型的当前加载的程序集。Gets the currently loaded assembly in which the specified type is defined.

public:
 static System::Reflection::Assembly ^ GetAssembly(Type ^ type);
public static System.Reflection.Assembly GetAssembly (Type type);
static member GetAssembly : Type -> System.Reflection.Assembly

参数

type
Type

一个对象,该对象表示将返回的程序集中的类型。An object representing a type in the assembly that will be returned.

返回

在其中定义指定类型的程序集。The assembly in which the specified type is defined.

异常

typenulltype is null.

示例

下面的示例检索包含 Int32 类型的程序集,并显示其名称和文件位置。The following example retrieves the assembly that contains the Int32 type and displays its name and file location.

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

注解

调用此方法等效于检索 Type.Assembly 属性的值。Calling this method is equivalent to retrieving the value of the Type.Assembly property. 不过,Type.Assembly 属性通常提供优异的性能。However, the Type.Assembly property typically offers superior performance.

若要调用此方法,必须具有一个 Type 对象,这意味着必须已加载定义该类的程序集。In order to call this method, you must have a Type object, which means that the assembly in which the class is defined must already be loaded.

安全性

ReflectionPermission
当通过 InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]) 这样的机制后期绑定调用时。when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). 关联的枚举: MemberAccessAssociated enumeration: MemberAccess

适用于