AssemblyVersionAttribute 類別

定義

指定使用屬性之組件的版本。Specifies the version of the assembly being attributed.

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
type AssemblyVersionAttribute = class
    inherit Attribute
Public NotInheritable Class AssemblyVersionAttribute
Inherits Attribute
繼承
AssemblyVersionAttribute
屬性

範例

下列範例會使用 AssemblyVersionAttribute 屬性,將版本號碼指派給元件。The following example uses the AssemblyVersionAttribute attribute to assign a version number to an assembly. 在編譯時期,此版本資訊會與元件的中繼資料一起儲存。At compile time, this version information is stored with the assembly's metadata. 在執行時間,此範例會在元件中找到的類型上,抓取 Type.Assembly 屬性的值,以取得執行中元件的參考,並從 Assembly.GetName 方法所傳回之 AssemblyName 物件的 Version 屬性中抓取元件的版本資訊。At run time, the example retrieves the value of the Type.Assembly property on a type found in the assembly to get a reference to the executing assembly, and it retrieves the assembly's version information from the Version property of the AssemblyName object returned by the Assembly.GetName method.

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 屬性是用來將版本號碼指派給元件。The AssemblyVersionAttribute attribute is used to assign a version number to an assembly. 該版本號碼接著會與元件的中繼資料一起儲存。That version number is then stored with the assembly's metadata.

元件版本號碼是元件身分識別的一部分,而且會在系結至元件和版本原則中扮演重要部分。The assembly version number is part of an assembly's identity and plays a key part in binding to the assembly and in version policy. Runtime 的預設版本原則為,除非被組態檔 (應用程式組態檔、發行者原則檔和電腦的系統管理員組態檔) 中的明確版本原則強制取代,否則應用程式只能搭配用來建置和測試它們的版本執行。The default version policy for the runtime is that applications run only with the versions they were built and tested with, unless overridden by explicit version policy in configuration files (the application configuration file, the publisher policy file, and the computer's administrator configuration file). 如需詳細資訊,請參閱.net 中的元件See Assemblies in .NET for more information.

注意

版本檢查只會發生於強式名稱的組件。Version checking only occurs with strong-named assemblies.

版本號碼有四個部分,如下所示:The version number has four parts, as follows:

<主要版本 >。<次要版本 >。<組建編號 >。<修訂 ><major version>.<minor version>.<build number>.<revision>

重要

版本的所有元件都必須是大於或等於0的整數。All components of the version must be integers greater than or equal to 0. 中繼資料會將元件的主要、次要、組建和修訂元件限制為 UInt16.MaxValue-1 的最大值。Metadata restricts the major, minor, build, and revision components for an assembly to a maximum value of UInt16.MaxValue - 1. 如果元件超過此值,就會發生編譯錯誤。If a component exceeds this value, a compilation error occurs.

您可以指定所有的值,也可以使用星號(*)接受預設組建編號、修訂編號或兩者。You can specify all the values or you can accept the default build number, revision number, or both by using an asterisk (*). 例如,[assembly:AssemblyVersion("2.3.25.1")] 會將2表示為主要版本、3做為次要版本、25作為組建編號,而1做為修訂編號。For example, [assembly:AssemblyVersion("2.3.25.1")] indicates 2 as the major version, 3 as the minor version, 25 as the build number, and 1 as the revision number. 版本號碼(例如 [assembly:AssemblyVersion("1.2.*")])會將1指定為主要版本,2為次要版本,並接受預設的組建和修訂編號。A version number such as [assembly:AssemblyVersion("1.2.*")] specifies 1 as the major version, 2 as the minor version, and accepts the default build and revision numbers. 版本號碼(例如 [assembly:AssemblyVersion("1.2.15.*")])會將1指定為主要版本、2為次要版本、15做為組建編號,並接受預設修訂編號。A version number such as [assembly:AssemblyVersion("1.2.15.*")] specifies 1 as the major version, 2 as the minor version, 15 as the build number, and accepts the default revision number. 預設組建編號會每日遞增。The default build number increments daily. 預設修訂編號是從當地時間午夜起算的秒數(不會考慮日光節約時間的時區調整),除以2。The default revision number is the number of seconds since midnight local time (without taking into account time zone adjustments for daylight saving time), divided by 2.

注意

如果您為組建編號指定星號,就無法指定修訂編號。If you specify an asterisk for the build number, you cannot specify a revision number.

元件的主要和次要版本會在元件匯出時當做類型程式庫版本號碼使用。The assembly major and minor versions are used as the type library version number when the assembly is exported. 某些 COM 主機不接受版本號碼為0.0 的類型程式庫。Some COM hosts do not accept type libraries with the version number 0.0. 因此,如果您想要將元件公開給 COM 用戶端,請在 [AssemblyVersionAttribute] 頁面中,將 [元件版本] 明確設定為 [1.0] (針對在 Visual Studio 2005Visual Studio 2005 以外建立的專案,而不指定 AssemblyVersionAttribute)。Therefore, if you want to expose an assembly to COM clients, set the assembly version explicitly to 1.0 in the AssemblyVersionAttribute page for projects created outside Visual Studio 2005Visual Studio 2005 and with no AssemblyVersionAttribute specified. 即使元件版本是0.0,也請執行此動作。Do this even when the assembly version is 0.0. Visual Studio 2005Visual Studio 2005 中建立的所有專案都有預設元件版本 1.0. *。All projects created in Visual Studio 2005Visual Studio 2005 have a default assembly version of 1.0.*.

若要取得您已載入之元件的名稱,請在元件上呼叫 GetName 以取得 AssemblyName,然後取得 Version 屬性。To get the name of an assembly you have loaded, call GetName on the assembly to get an AssemblyName, and then get the Version property. 若要取得您尚未載入之元件的名稱,請從您的用戶端應用程式呼叫 GetAssemblyName,以檢查您的應用程式所使用的元件版本。To get the name of an assembly you have not loaded, call GetAssemblyName from your client application to check the assembly version that your application uses.

AssemblyVersionAttribute 屬性只能套用一次。The AssemblyVersionAttribute attribute can only be applied once. 某些 Visual Studio 專案範本已包含屬性。Some Visual Studio project templates already include the attribute. 在這些專案中,在程式碼中加入屬性會導致編譯器錯誤。In those projects, adding the attribute in code causes a compiler error.

建構函式

AssemblyVersionAttribute(String)

使用正在屬性化之組件的版本號碼,初始化 AssemblyVersionAttribute 類別的新執行個體。Initializes a new instance of the AssemblyVersionAttribute class with the version number of the assembly being attributed.

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)
Version

取得使用屬性之組件的版本號碼。Gets the version number of the attributed assembly.

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

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

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

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

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

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

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)

適用於

另請參閱