AssemblyVersionAttribute 類別

定義

指定使用屬性之組件的版本。

public ref class AssemblyVersionAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly, Inherited=false)]
public sealed class AssemblyVersionAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false)]
public sealed class AssemblyVersionAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class AssemblyVersionAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly, Inherited=false)>]
type AssemblyVersionAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false)>]
type AssemblyVersionAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type AssemblyVersionAttribute = class
    inherit Attribute
Public NotInheritable Class AssemblyVersionAttribute
Inherits Attribute
繼承
AssemblyVersionAttribute
屬性

範例

下列範例會 AssemblyVersionAttribute 使用 屬性,將版本號碼指派給元件。 在編譯時期,這個版本資訊會與元件的中繼資料一起儲存。 在執行時間,此範例 Type.Assembly 會擷取元件中找到之類型上的 屬性值,以取得執行中元件的參考,並從 方法所 Assembly.GetName 傳回物件的 屬性 AssemblyName 擷取元件的版本資訊 Version

using System;
using System.Reflection;

[assembly:AssemblyVersionAttribute("2.0.1")]

public class Example1
{
   public static void Main()
   {
      Assembly thisAssem = typeof(Example1).Assembly;
      AssemblyName thisAssemName = thisAssem.GetName();
       
      Version ver = thisAssemName.Version;
       
      Console.WriteLine("This is version {0} of {1}.", ver, thisAssemName.Name);    
   }
}
// The example displays the following output:
//        This is version 2.0.1.0 of Example1.
Imports System.Reflection

<Assembly:AssemblyVersionAttribute("2.0.1")>
Module Example1
   Public Sub Main()
       Dim thisAssem As Assembly = GetType(Example1).Assembly
       Dim thisAssemName As AssemblyName = thisAssem.GetName()
       
       Dim ver As Version = thisAssemName.Version
       
       Console.WriteLine("This is version {0} of {1}.", ver, thisAssemName.Name)    
   End Sub
End Module
' The example displays the following output:
'      This is version 2.0.1.0 of Example1.

備註

屬性 AssemblyVersionAttribute 可用來將版本號碼指派給元件。 該版本號碼接著會與元件的中繼資料一起儲存。

元件版本號碼是元件身分識別的一部分,在系結至元件和版本原則中扮演重要部分。 Runtime 的預設版本原則為,除非被組態檔 (應用程式組態檔、發行者原則檔和電腦的系統管理員組態檔) 中的明確版本原則強制取代,否則應用程式只能搭配用來建置和測試它們的版本執行。 如需詳細資訊 ,請參閱 .NET 中的元件

注意

版本檢查只會發生於強式名稱的組件。

版本號碼有四個部分,如下所示:

<主要版本 > 。 <次要版本 > 。 <組建編號 > 。 <修訂>

重要

版本的所有元件都必須是大於或等於 0 的整數。 中繼資料會將元件的主要、次要、建置和修訂元件限制為最大值 UInt16.MaxValue - 1。 如果元件超過此值,就會發生編譯錯誤。

您可以使用星號 (*) 來指定所有值,也可以接受預設組建編號、修訂編號或兩者。 例如, [assembly:AssemblyVersion("2.3.25.1")] 將 2 表示為主要版本、3 做為次要版本、25 做為組建編號,1 表示為修訂編號。 版本號碼,例如 [assembly:AssemblyVersion("1.2.*")] 將 1 指定為主要版本、2 做為次要版本,並接受預設組建和修訂編號。 版本號碼,例如 [assembly:AssemblyVersion("1.2.15.*")] 將 1 指定為主要版本、2 做為次要版本、15 做為組建編號,並接受預設修訂編號。 預設組建編號會每日遞增。 預設修訂編號是自午夜當地時間起的秒數 (,而不考慮日光節約時間) 的時區調整,除以 2。

注意

如果您為組建編號指定星號,則無法指定修訂編號。

匯出元件時,元件主要和次要版本會當做類型程式庫版本號碼使用。 某些 COM 主機不接受版本號碼為 0.0 的類型程式庫。 因此,如果您想要將元件公開給 COM 用戶端,請在頁面中明確將元件版本設定為 1.0 AssemblyVersionAttribute ,以取得在 Visual Studio 2005 外部建立且未 AssemblyVersionAttribute 指定的專案。 即使元件版本為 0.0,也請執行此動作。 在 Visual Studio 2005 中建立的所有專案都有預設元件版本 1.0.*。

若要取得您已載入的元件名稱,請在元件上呼叫 GetName 以取得 ,然後取得 AssemblyNameVersion 屬性。 若要取得您尚未載入的元件名稱,請從用戶端應用程式呼叫 GetAssemblyName ,以檢查應用程式所使用的元件版本。

屬性 AssemblyVersionAttribute 只能套用一次。 某些 Visual Studio 專案範本已經包含 屬性。 在這些專案中,在程式碼中新增 屬性會導致編譯器錯誤。

建構函式

AssemblyVersionAttribute(String)

使用正在屬性化之組件的版本號碼,初始化 AssemblyVersionAttribute 類別的新執行個體。

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
Version

取得使用屬性之組件的版本號碼。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

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

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱