共用方式為


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 用於為組裝體指派版本號。 該版本號會與組合的元資料一同儲存。

組合語言版本號是組裝裝置身份的一部分,並在綁定組裝檔及版本政策中扮演關鍵角色。 運行時的預設版本原則是,應用程式只會使用其建置和測試的版本來執行,除非在組態檔中有明確版本原則來覆寫(此組態檔包括應用程式組態檔、發行者原則檔,以及電腦系統管理員的組態檔)。 更多資訊請參見 .NET 中的組裝套件

備註

版本檢查僅在具強名稱的元件中進行。

版本號分為四個部分,分別如下:

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

這很重要

該版本的所有分量必須是大於或等於 0 的整數。 元資料限制組件的主要、次要、建置及修訂元件最大值 UInt16.MaxValue 為 -1。 若元件超過此值,則會發生編譯錯誤。

例如,表示 [assembly:AssemblyVersion("2.3.25.1")] 2 為主要版本,3 為次要版本,25 為製造號,1 為修訂號。

屬性 AssemblyVersionAttribute 允許你指定星號(*)來取代建置號或修訂編號。 像 這樣的 [assembly:AssemblyVersion("1.2.*")] 版本號會指定 1 為主要版本,2 為次要版本,並接受預設的編譯號與修訂編號。 版本號如 指定 [assembly:AssemblyVersion("1.2.15.*")] 1 為主要版本,2 為次要版本,15 為組裝號,並接受預設的修訂編號。 預設的組裝編號每天都會增加。 預設的修訂數是自當地時間午夜起的秒數(未考慮夏令時間的時區調整)除以2。 如果你指定了製作號星號,就無法指定修訂號。

這很重要

使用 AssemblyVersionAttribute 指定星號屬性的條件:

  • 使建置輸出不可重現(參見可重現建構)。 如果專案設定 Deterministic build 屬性為 true 錯誤 CS8357 ,編譯器會回報錯誤。
  • 可能會降低建置效能,因為會阻止建置檔快取編譯器的輸出。
  • 編輯繼續熱載入 功能不相容。

你可以透過限制使用時間基礎版本,讓使用條件編譯來釋出版本,來緩解部分問題,例如:

#if DEBUG
[assembly: AssemblyVersion("1.0.0.0")]
#else
[assembly: AssemblyVersion("1.0.*")]
#endif

更好的版本管理方法是從 HEAD 提交 SHA(針對 git 倉庫)推導組合語言或檔案版本。 例如,請參考 Nerdbank.GitVersioning

匯出組件時,組合語言的主要版本與次要版本作為型別庫的版本號。 部分 COM 主機不接受版本號為 0.0 的型別函式庫。 因此,如果你想將組合語言暴露給 COM 客戶端,請在頁面中 AssemblyVersionAttribute 明確將組合語言版本設為 1.0,適用於在 Visual Studio 2005 以外建立且未 AssemblyVersionAttribute 指定的任何專案。 即使組合語言版本是 0.0 也要這麼做。 所有在 Visual Studio 2005 建立的專案預設組合語言版本為 1.0.*。

要取得你已載入的組件名稱,請呼叫 GetName 該組件取得 AssemblyName,然後取得該 Version 屬性。 要取得你尚未載入的組合語言名稱,請從客戶端應用程式呼叫 GetAssemblyName ,查詢該應用程式使用的組合語言版本。

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

建構函式

名稱 Description
AssemblyVersionAttribute(String)

初始化類別的新實例 AssemblyVersionAttribute ,並賦予該組合的版本號。

屬性

名稱 Description
TypeId

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

(繼承來源 Attribute)
Version

會取得該組合的版本號。

方法

名稱 Description
Equals(Object)

傳回值,這個值表示這個實例是否等於指定的物件。

(繼承來源 Attribute)
GetHashCode()

傳回這個實例的哈希碼。

(繼承來源 Attribute)
GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

名稱 Description
_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)

適用於

另請參閱