Assembly 類別

定義

表示組件 (Assembly),此組件是可重複使用、可控制版本和自我描述的 Common Language Runtime 應用程式建置區塊。

public ref class Assembly abstract
public ref class Assembly abstract : System::Reflection::ICustomAttributeProvider, System::Runtime::Serialization::ISerializable
public ref class Assembly abstract : System::Reflection::ICustomAttributeProvider
public ref class Assembly : System::Reflection::ICustomAttributeProvider, System::Runtime::InteropServices::_Assembly, System::Runtime::Serialization::ISerializable, System::Security::IEvidenceFactory
public ref class Assembly abstract : System::Reflection::ICustomAttributeProvider, System::Runtime::InteropServices::_Assembly, System::Runtime::Serialization::ISerializable, System::Security::IEvidenceFactory
public abstract class Assembly
public abstract class Assembly : System.Reflection.ICustomAttributeProvider, System.Runtime.Serialization.ISerializable
public abstract class Assembly : System.Reflection.ICustomAttributeProvider
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
public class Assembly : System.Reflection.ICustomAttributeProvider, System.Runtime.InteropServices._Assembly, System.Runtime.Serialization.ISerializable, System.Security.IEvidenceFactory
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class Assembly : System.Reflection.ICustomAttributeProvider, System.Runtime.InteropServices._Assembly, System.Runtime.Serialization.ISerializable, System.Security.IEvidenceFactory
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Assembly : System.Reflection.ICustomAttributeProvider, System.Runtime.InteropServices._Assembly, System.Runtime.Serialization.ISerializable, System.Security.IEvidenceFactory
type Assembly = class
type Assembly = class
    interface ICustomAttributeProvider
    interface ISerializable
type Assembly = class
    interface ICustomAttributeProvider
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
type Assembly = class
    interface _Assembly
    interface IEvidenceFactory
    interface ICustomAttributeProvider
    interface ISerializable
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Assembly = class
    interface _Assembly
    interface IEvidenceFactory
    interface ICustomAttributeProvider
    interface ISerializable
Public MustInherit Class Assembly
Public MustInherit Class Assembly
Implements ICustomAttributeProvider, ISerializable
Public MustInherit Class Assembly
Implements ICustomAttributeProvider
Public Class Assembly
Implements _Assembly, ICustomAttributeProvider, IEvidenceFactory, ISerializable
Public MustInherit Class Assembly
Implements _Assembly, ICustomAttributeProvider, IEvidenceFactory, ISerializable
繼承
Assembly
衍生
屬性
實作

範例

下列程式代碼範例示範如何取得目前正在執行的元件、建立包含在該元件中的型別實例,以及使用晚期系結叫用其中一個型別的方法。 為了達到此目的,程式代碼範例會定義名為 Example的類別,並使用名為 SampleMethod的方法。 類別的建構函式接受整數,用來計算 方法的傳回值。

程式代碼範例也會示範如何使用 GetName 方法來取得 AssemblyName 可用來剖析元件完整名稱的物件。 此範例會顯示元件、 CodeBase 屬性和 EntryPoint 屬性的版本號碼。

using namespace System;
using namespace System::Reflection;
using namespace System::Security::Permissions;

[assembly:AssemblyVersionAttribute("1.0.2000.0")];

public ref class Example
{
private: 
    int factor;

public:
    Example(int f)
    {
        factor = f;
    }

    int SampleMethod(int x) 
    { 
        Console::WriteLine("\nExample->SampleMethod({0}) executes.", x);
        return x * factor;
    }
};

void main()
{
    Assembly^ assem = Example::typeid->Assembly;

    Console::WriteLine("Assembly Full Name:");
    Console::WriteLine(assem->FullName);

    // The AssemblyName type can be used to parse the full name.
    AssemblyName^ assemName = assem->GetName();
    Console::WriteLine("\nName: {0}", assemName->Name);
    Console::WriteLine("Version: {0}.{1}", 
        assemName->Version->Major, assemName->Version->Minor);

    Console::WriteLine("\nAssembly CodeBase:");
    Console::WriteLine(assem->CodeBase);

    // Create an object from the assembly, passing in the correct number and
    // type of arguments for the constructor.
    Object^ o = assem->CreateInstance("Example", false, 
        BindingFlags::ExactBinding, 
        nullptr, gcnew array<Object^> { 2 }, nullptr, nullptr);

    // Make a late-bound call to an instance method of the object.    
    MethodInfo^ m = assem->GetType("Example")->GetMethod("SampleMethod");
    Object^ ret = m->Invoke(o, gcnew array<Object^> { 42 });
    Console::WriteLine("SampleMethod returned {0}.", ret);

    Console::WriteLine("\nAssembly entry point:");
    Console::WriteLine(assem->EntryPoint);
}

/* This code example produces output similar to the following:

Assembly Full Name:
source, Version=1.0.2000.0, Culture=neutral, PublicKeyToken=null

Name: source
Version: 1.0

Assembly CodeBase:
file:///C:/sdtree/AssemblyClass/cpp/source.exe

Example->SampleMethod(42) executes.
SampleMethod returned 84.

Assembly entry point:
UInt32 _mainCRTStartup()
 */
using System;
using System.Reflection;
using System.Security.Permissions;

[assembly:AssemblyVersionAttribute("1.0.2000.0")]

public class Example
{
    private int factor;
    public Example(int f)
    {
        factor = f;
    }

    public int SampleMethod(int x)
    {
        Console.WriteLine("\nExample.SampleMethod({0}) executes.", x);
        return x * factor;
    }

    public static void Main()
    {
        Assembly assem = typeof(Example).Assembly;

        Console.WriteLine("Assembly Full Name:");
        Console.WriteLine(assem.FullName);

        // The AssemblyName type can be used to parse the full name.
        AssemblyName assemName = assem.GetName();
        Console.WriteLine("\nName: {0}", assemName.Name);
        Console.WriteLine("Version: {0}.{1}",
            assemName.Version.Major, assemName.Version.Minor);

        Console.WriteLine("\nAssembly CodeBase:");
        Console.WriteLine(assem.CodeBase);

        // Create an object from the assembly, passing in the correct number
        // and type of arguments for the constructor.
        Object o = assem.CreateInstance("Example", false,
            BindingFlags.ExactBinding,
            null, new Object[] { 2 }, null, null);

        // Make a late-bound call to an instance method of the object.
        MethodInfo m = assem.GetType("Example").GetMethod("SampleMethod");
        Object ret = m.Invoke(o, new Object[] { 42 });
        Console.WriteLine("SampleMethod returned {0}.", ret);

        Console.WriteLine("\nAssembly entry point:");
        Console.WriteLine(assem.EntryPoint);
    }
}

/* This code example produces output similar to the following:

Assembly Full Name:
source, Version=1.0.2000.0, Culture=neutral, PublicKeyToken=null

Name: source
Version: 1.0

Assembly CodeBase:
file:///C:/sdtree/AssemblyClass/cs/source.exe

Example.SampleMethod(42) executes.
SampleMethod returned 84.

Assembly entry point:
Void Main()
 */
Imports System.Reflection
Imports System.Security.Permissions

<assembly: AssemblyVersionAttribute("1.0.2000.0")>

Public Class Example
    Private factor As Integer
    
    Public Sub New(ByVal f As Integer) 
        factor = f
    End Sub 
    
    Public Function SampleMethod(ByVal x As Integer) As Integer 
        Console.WriteLine(vbCrLf & "Example.SampleMethod({0}) executes.", x)
        Return x * factor
    End Function 
    
    Public Shared Sub Main() 
        Dim assem As Assembly = GetType(Example).Assembly
        
        Console.WriteLine("Assembly Full Name:")
        Console.WriteLine(assem.FullName)
        
        ' The AssemblyName type can be used to parse the full name.
        Dim assemName As AssemblyName = assem.GetName()
        Console.WriteLine(vbLf + "Name: {0}", assemName.Name)
        Console.WriteLine("Version: {0}.{1}", assemName.Version.Major, _
            assemName.Version.Minor)
        
        Console.WriteLine(vbLf + "Assembly CodeBase:")
        Console.WriteLine(assem.CodeBase)
        
        ' Create an object from the assembly, passing in the correct number
        ' and type of arguments for the constructor.
        Dim o As Object = assem.CreateInstance("Example", False, _
            BindingFlags.ExactBinding, Nothing, _
            New Object() { 2 }, Nothing, Nothing)
        
        ' Make a late-bound call to an instance method of the object.    
        Dim m As MethodInfo = assem.GetType("Example").GetMethod("SampleMethod")
        Dim ret As Object = m.Invoke(o, New Object() { 42 })
        Console.WriteLine("SampleMethod returned {0}.", ret)
        
        Console.WriteLine(vbCrLf & "Assembly entry point:")
        Console.WriteLine(assem.EntryPoint)
    
    End Sub 
End Class 

' This code example produces output similar to the following:
'
'Assembly Full Name:
'source, Version=1.0.2000.0, Culture=neutral, PublicKeyToken=null
'
'Name: source
'Version: 1.0
'
'Assembly CodeBase:
'file:///C:/sdtree/AssemblyClass/vb/source.exe
'
'Example.SampleMethod(42) executes.
'SampleMethod returned 84.
'
'Assembly entry point:
'Void Main()
'

備註

Assembly使用 類別來載入元件、探索元件的元數據和組成部分、探索元件中包含的類型,以及建立這些類型的實例。

若要取得物件數位, Assembly 代表目前載入至應用程式域的元件 (例如,簡單專案的預設應用程式域) ,請使用 AppDomain.GetAssemblies 方法。

若要動態載入元件,類別 Assembly 會在Visual Basic) 中提供下列靜態方法 (Shared 方法。 元件會載入載入作業所在的應用程式域。

  • 載入元件的建議方法是使用 Load 方法來識別要以其顯示名稱載入的元件 (,例如 “System.Windows.Forms, Version=2.0.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”) 。 搜尋元件會遵循運行時間 如何找出元件中所述的規則。

  • ReflectionOnlyLoadReflectionOnlyLoadFrom 方法可讓您載入元件以進行反映,但無法用於執行。 例如,以64位平臺為目標的元件可由在32位平臺上執行的程式代碼進行檢查。

  • LoadFileLoadFrom 方法是針對必須依路徑識別元件的罕見案例所提供。

若要取得 Assembly 目前執行之元件的物件,請使用 GetExecutingAssembly 方法。

類別的許多 Assembly 成員都會提供元件的相關信息。 例如:

方法 GetTypes 會列出元件中的所有類型。 方法 GetExportedTypes 會列出元件外部呼叫端可以看到的類型。 方法 GetType 可用來搜尋元件中的特定類型。 方法 CreateInstance 可用來在元件中搜尋和建立類型的實例。

For more information on assemblies, see the "Application Domains and Assemblies" section in the Application Domains topic.

建構函式

Assembly()

初始化 Assembly 類別的新執行個體。

屬性

CodeBase
已淘汰.
已淘汰.

取得組件位置,例如原先在 AssemblyName 物件中指定的。

CustomAttributes

取得包含此組件之自訂屬性的集合。

DefinedTypes

取得這個組件中定義之類型的集合。

EntryPoint

取得這個組件的進入點。

EscapedCodeBase
已淘汰.
已淘汰.

取得代表基礎碼的 URI,包括逸出字元。

Evidence

取得這個組件的辨識碼。

ExportedTypes

取得在這個組件中定義的公用類型集合,而這些類型在組件外部是可見的。

FullName

取得組件的顯示名稱。

GlobalAssemblyCache
已淘汰.

取得值,指出是否只從全域程式集緩存載入元件 (.NET Framework) 。

HostContext

取得用來載入組件的主應用程式內容。

ImageRuntimeVersion

取得字串,表示儲存在含有資訊清單之檔案中的 Common Language Runtime (CLR) 版本。

IsCollectible

取得指出此組件是否在可回收 AssemblyLoadContext 中保存的值。

IsDynamic

取得值,這個值表示目前組件是否使用反映發出在目前處理序中動態產生。

IsFullyTrusted

取得值,這個值表示目前組件是否以完全信任來載入。

Location

取得包含資訊清單之載入檔的完整路徑或 UNC 位置。

ManifestModule

取得包含目前組件之資訊清單的模組。

Modules

取得包含這個組件中模組的集合。

PermissionSet

取得目前組件的授權集。

ReflectionOnly

取得 Boolean 值,指出這個組件是否已載入僅限反映的內容中。

SecurityRuleSet

取得值,這個值指出應針對此組件強制執行的 Common Language Runtime (CLR) 安全性規則組合。

方法

CreateInstance(String)

從這個組件找出指定類型,並使用系統啟動項,利用區分大小寫的搜尋,建立它的執行個體。

CreateInstance(String, Boolean)

從這個組件找出指定類型,並使用系統啟動項,利用選擇性區分大小寫的搜尋,建立它的執行個體。

CreateInstance(String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

從這個組件找出指定類型,並使用系統啟動項,利用選擇性區分大小寫的搜尋,以及取得指定文化特性 (Culture)、引數和繫結和啟動屬性,建立它的執行個體。

CreateQualifiedName(String, String)

建立類型名稱,由其組件的顯示名稱所限定。

Equals(Object)

判斷這個組件和指定的物件是否相等。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetAssembly(Type)

取得目前載入的組件,其中定義指定類型。

GetCallingAssembly()

傳回方法的 Assembly,其叫用目前執行的方法。

GetCustomAttributes(Boolean)

取得這個組件的所有自訂屬性。

GetCustomAttributes(Type, Boolean)

取得這個組件由類型所指定的自訂屬性。

GetCustomAttributesData()

傳回已套用至目前 Assembly 之屬性的相關資訊,表示為 CustomAttributeData 物件。

GetEntryAssembly()

取得預設應用程式定義域中的處理序可執行檔。 在其他應用程式定義域中,這是由 ExecuteAssembly(String) 執行的第一個可執行檔。

GetExecutingAssembly()

取得組件,其中含有目前正在執行的程式碼。

GetExportedTypes()

取得在這個組件中定義的公用類型,而這些類型在組件外部是可見的。

GetFile(String)

取得指定之檔案的 FileStream,這個指定之檔案位於這個組件的資訊清單檔案表中。

GetFiles()

取得組件資訊清單檔案表中的檔案。

GetFiles(Boolean)

取得組件資訊清單檔案表中的檔案,指定是否要包含資源模組。

GetForwardedTypes()

表示組件 (Assembly),此組件是可重複使用、可控制版本和自我描述的 Common Language Runtime 應用程式建置區塊。

GetHashCode()

傳回這個執行個體的雜湊碼。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLoadedModules()

取得做為這個組件部分的所有載入模組。

GetLoadedModules(Boolean)

取得做為這個組件部分的所有載入模組,指定是否要包含資源模組。

GetManifestResourceInfo(String)

傳回指定資源已保存方式的資訊。

GetManifestResourceNames()

傳回這個組件中的所有資源名稱。

GetManifestResourceStream(String)

載入來自這個組件的指定資訊清單資源。

GetManifestResourceStream(Type, String)

從這個組件載入指定資訊清單資源,由指定類型的命名空間限定範圍。

GetModule(String)

取得這個組件中的指定模組。

GetModules()

取得所有屬於這個組件的模組。

GetModules(Boolean)

取得所有屬於這個組件的模組,指定是否要包含資源模組。

GetName()

取得這個組件的 AssemblyName

GetName(Boolean)

取得這個組件的 AssemblyName,設定按照 copiedName 指定的基礎碼。

GetObjectData(SerializationInfo, StreamingContext)
已淘汰.

使用要重新執行個體化這個組件所需的所有資料,取得序列化 (Serialization) 資訊。

GetReferencedAssemblies()

取得這個組件參考之所有組件的 AssemblyName 物件。

GetSatelliteAssembly(CultureInfo)

取得指定文化特性的附屬組件。

GetSatelliteAssembly(CultureInfo, Version)

取得指定文化特性的附屬組件指定版本。

GetType()

表示組件 (Assembly),此組件是可重複使用、可控制版本和自我描述的 Common Language Runtime 應用程式建置區塊。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
GetType(String)

取得具有組件執行個體中指定名稱的 Type 物件。

GetType(String, Boolean)

取得具有組件執行個體中指定之名稱的 Type 物件,並在找不到類型時選擇性地擲回例外狀況。

GetType(String, Boolean, Boolean)

使用忽略大小寫和找不到類型時擲回例外狀況的選項,取得具有組件執行個體中指定之名稱的 Type 物件。

GetTypes()

取得這個元件中定義的所有類型。

IsDefined(Type, Boolean)

指出是否已將指定的屬性套用至組件。

Load(AssemblyName)

載入組件,指定其 AssemblyName

Load(AssemblyName, Evidence)
已淘汰.

載入組件,指定其 AssemblyName。 元件會使用提供的辨識項載入。

Load(Byte[])

載入具有通用物件檔案格式 (Common Object File Format,COFF) 之映像的組件 (包含發出的組件)。

Load(Byte[], Byte[])

使用通用物件檔案格式 (COFF) 映像載入組件,其中包含已發出的組件,並可選擇性地包含組件的符號。

Load(Byte[], Byte[], Evidence)
已淘汰.

使用通用物件檔案格式 (COFF) 映像載入組件,其中包含已發出的組件,並可選擇性地包含組件的符號和辨識項。

Load(Byte[], Byte[], SecurityContextSource)

使用通用物件檔案格式 (COFF) 映像載入組件,其中包含已發出的組件,並可選擇性地包含組件的符號,以及指定安全性內容的來源。

Load(String)

載入具有指定名稱的組件。

Load(String, Evidence)
已淘汰.

載入元件,指定其顯示名稱,並使用提供的辨識項。

LoadFile(String)

載入指定路徑上組件檔案的內容。

LoadFile(String, Evidence)
已淘汰.

載入具有其路徑的元件,並使用提供的辨識項載入元件。

LoadFrom(String)

載入組件,指定其檔案名稱或路徑。

LoadFrom(String, Byte[], AssemblyHashAlgorithm)

指定組件的檔案名稱或路徑,雜湊值和雜湊演算法,來載入組件。

LoadFrom(String, Evidence)
已淘汰.

載入組件,指定其檔案名稱或路徑,並提供安全性辨識項。

LoadFrom(String, Evidence, Byte[], AssemblyHashAlgorithm)
已淘汰.

指定組件的檔案名稱或路徑、安全性辨識項、雜湊值和雜湊演算法,來載入組件。

LoadModule(String, Byte[])

使用通用物件檔案格式 (COFF) 為基礎的映像 (包含發出的模組) 或是資源檔,載入組件的內部模組。

LoadModule(String, Byte[], Byte[])

使用通用物件檔案格式 (COFF) 為基礎的映像 (包含發出的模組) 或是資源檔,載入組件的內部模組。 表示模組符號的未經處理位元組也載入。

LoadWithPartialName(String)
已淘汰.
已淘汰.
已淘汰.

使用部分的名稱,從應用程式目錄或從全域組件快取載入組件。

LoadWithPartialName(String, Evidence)
已淘汰.

使用部分的名稱,從應用程式目錄或從全域組件快取載入組件。 元件會使用提供的辨識項載入。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ReflectionOnlyLoad(Byte[])
已淘汰.

從以通用物件檔案格式 (COFF) 為基礎的映像 (包含發出的組件) 載入組件。 這個組件會載入呼叫端應用程式定義域之僅限反映的內容中。

ReflectionOnlyLoad(String)
已淘汰.

將組件載入僅限反映的內容中,並指定其顯示名稱。

ReflectionOnlyLoadFrom(String)
已淘汰.

將組件載入僅限反映的內容中,並指定其路徑。

ToString()

傳回組件的完整名稱,也稱為顯示名稱。

UnsafeLoadFrom(String)

將組件載入到載入內容中,並略過部分安全性檢查。

運算子

Equality(Assembly, Assembly)

表示兩個 Assembly 物件是否相等。

Inequality(Assembly, Assembly)

表示兩個 Assembly 物件是否不相等。

事件

ModuleResolve

發生於 Common Language Runtime 類別載入器無法經由一般方法解析對組件內部模組的參考時。

明確介面實作

_Assembly.GetType()

傳回目前執行個體的類型。

ICustomAttributeProvider.GetCustomAttributes(Boolean)

傳回這個成員中定義的所有自訂屬性的陣列 (但具名屬性除外),如果沒有自訂屬性,則傳回空陣列。

ICustomAttributeProvider.GetCustomAttributes(Type, Boolean)

傳回這個成員中定義的自訂屬性陣列 (依類型識別),如果沒有該類型的自訂屬性,則傳回空陣列。

ICustomAttributeProvider.IsDefined(Type, Boolean)

指出此成員上是否有定義一個或多個 attributeType 執行個體。

擴充方法

GetExportedTypes(Assembly)

表示組件 (Assembly),此組件是可重複使用、可控制版本和自我描述的 Common Language Runtime 應用程式建置區塊。

GetModules(Assembly)

表示組件 (Assembly),此組件是可重複使用、可控制版本和自我描述的 Common Language Runtime 應用程式建置區塊。

GetTypes(Assembly)

表示組件 (Assembly),此組件是可重複使用、可控制版本和自我描述的 Common Language Runtime 應用程式建置區塊。

GetCustomAttribute(Assembly, Type)

擷取指定型別的自訂屬性,此屬性套用至指定組件。

GetCustomAttribute<T>(Assembly)

擷取指定型別的自訂屬性,此屬性套用至指定組件。

GetCustomAttributes(Assembly)

擷取套用至指定組件的自訂屬性集合。

GetCustomAttributes(Assembly, Type)

擷取指定型別的自訂屬性集合,此集合套用至指定組件。

GetCustomAttributes<T>(Assembly)

擷取指定型別的自訂屬性集合,此集合套用至指定組件。

IsDefined(Assembly, Type)

指出是否將所指定型別的自訂屬性套用至指定的組件。

TryGetRawMetadata(Assembly, Byte*, Int32)

擷取元件的元數據區段,以搭配 MetadataReader使用。

適用於

執行緒安全性

此型別具備執行緒安全。

另請參閱