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 作为修订号。 版本号(如) [assembly:AssemblyVersion("1.2.*")] 将 1 指定为主版本,将 2 指定为次要版本,并接受默认内部版本号和修订号。 版本号(例如) [assembly:AssemblyVersion("1.2.15.*")] 将 1 指定为主版本,将 2 指定为次要版本,将 15 指定为内部版本号,并接受默认修订号。 默认内部版本号每天递增。 默认修订号是自当地时间午夜 (以来的秒数,而不考虑夏令时) 时区调整,除以 2。

注意

如果为内部版本号指定星号,则无法指定修订号。

导出程序集时,程序集主版本和次要版本将用作类型库版本号。 某些 COM 主机不接受版本号为 0.0 的类型库。 因此,如果要向 COM 客户端公开程序集,请在 Visual Studio 2005 外部创建且未指定项目AssemblyVersionAttribute的页面中AssemblyVersionAttribute将程序集版本显式设置为 1.0。 即使程序集版本为 0.0,也是如此。 在 Visual Studio 2005 中创建的所有项目的默认程序集版本为 1.0.*。

若要获取已加载的程序集的名称,请在程序集上调用 GetName 以获取 , AssemblyName然后获取 Version 属性。 若要获取尚未加载的程序集的名称,请从客户端应用程序调用 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)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅