Assembly.GetAssembly(Type) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得目前載入的組件,其中定義指定類型。
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
物件,代表會傳回的組件類型。
傳回
組件,其中定義指定類型。
例外狀況
type
為 null
。
範例
下列範例會擷取包含型別的 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 物件,這表示必須已經載入定義類別的元件。