AssemblyVersionAttribute 类

定义

指定正在属性化的程序集的版本。Specifies the version of the assembly being attributed.

public ref class AssemblyVersionAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false)]
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找到的类型的属性的值,以获取对正在执行的程序集的引用,并Version从的AssemblyName属性中检索该程序集的版本信息。Assembly.GetName方法返回的对象。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. 除非被配置文件(应用程序配置文件、发行者策略文件和计算机的管理员配置文件)中的显式版本策略重写,否则运行时的默认版本策略是,应用程序只与它们生成和测试时所用的程序集版本一起运行。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). 有关详细信息,请参阅公共语言运行时中的程序集See Assemblies in the Common Language Runtime 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 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式界面实现

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

将一组名称映射为对应的一组调度标识符。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)

适用于

另请参阅