AssemblyName 类

完整描述程序集的唯一标识。

**命名空间:**System.Reflection
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class AssemblyName
    Implements _AssemblyName, ICloneable, ISerializable, IDeserializationCallback
用法
Dim instance As AssemblyName
[SerializableAttribute] 
[ClassInterfaceAttribute(ClassInterfaceType.None)] 
[ComVisibleAttribute(true)] 
public sealed class AssemblyName : _AssemblyName, ICloneable, ISerializable, 
    IDeserializationCallback
[SerializableAttribute] 
[ClassInterfaceAttribute(ClassInterfaceType::None)] 
[ComVisibleAttribute(true)] 
public ref class AssemblyName sealed : _AssemblyName, ICloneable, ISerializable, 
    IDeserializationCallback
/** @attribute SerializableAttribute() */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class AssemblyName implements _AssemblyName, ICloneable, 
    ISerializable, IDeserializationCallback
SerializableAttribute 
ClassInterfaceAttribute(ClassInterfaceType.None) 
ComVisibleAttribute(true) 
public final class AssemblyName implements _AssemblyName, ICloneable, 
    ISerializable, IDeserializationCallback

备注

AssemblyName 对象包含有关程序集的信息,在绑定到该程序集时可以使用此信息。程序集的标识由以下部分组成:

  • 简单名称

  • 版本号

  • 加密密钥对

  • 支持的区域性

简单名称通常是清单文件不带扩展名的文件名。密钥对包括公钥和私钥,用于创建程序集的强名称签名。

所有支持公共语言运行库的编译器将发出嵌套类的简单名称,并且当被查询时,反射将依照下列约定构造一个 mangled 名称。

分隔符

含义

反斜杠 (\)

转义符。

逗号 (,)

位于程序集名称前。

加号 (+)

位于嵌套类前。

例如,类的完全限定名可能类似于如下形式:

ContainingClass+NestedClass,MyAssembly

“++”变成“\+\+”,而“\”变成“\\”。

可以保持此限定名并在以后使用它来加载 Type。若要搜索和加载 Type,请只结合类型名或者结合程序集限定类型名使用 GetType。只结合类型名时,GetType 将只在调用方的程序集中查找 Type,然后再在系统程序集中查找。结合程序集限定类型名时,GetType 将在所有程序集中查找 Type

完全指定的 AssemblyName 必须具有名称、区域性、公钥或公钥标记、主版本、次版本、内部版本号和修订号参数。最后四个参数封装在 Version 类型中。

若要创建简单名称,请使用默认构造函数创建一个 AssemblyName 对象并设置 Name。其他属性可选。

若要创建完整的强名称,请使用默认构造函数创建一个 AssemblyName 对象并设置 NameKeyPair。其他属性可选。使用 SetPublicKeySetPublicKeyToken 设置公钥和强名称。强名称签名始终使用 SHA1 哈希算法。

若要确保正确地构建名称,请使用以下属性:

还可以通过使用带 /l 选项的 全局程序集缓存工具 (Gacutil.exe) 获取名称

对于部分指定的强名称,使用默认构造函数创建 AssemblyName 对象并设置名称和公钥。可在以后使用程序集链接器 (Al.exe) 对使用此 AssemblyName 创建的程序集进行签名。

指定的公钥和 KeyPair 值可以不一致。这对开发人员方案很有用。在这种情况下,由用 GetPublicKey 检索的公钥指定正确的公钥,而由 KeyPair 指定开发期间所使用的公钥和私钥。当运行时检测到 KeyPair 和公钥不匹配时,它会在注册表中查找与公钥匹配的正确密钥。

AssemblyName 的显示名称的格式是以名称开头并由逗号分隔的 Unicode 字符串,如下所示:

Name <,Culture = CultureInfo> <,Version = Major.Minor.Build.Revision> <, StrongName> <,PublicKeyToken> '\0'

Name 是程序集的文本名称。CultureInfo 是 RFC1766 格式定义的区域性。MajorMinorBuildRevision 分别是程序集的主版本、次版本、内部版本号和修订号。StrongName 是使用 SHA-1 哈希算法生成的公钥和由 SetPublicKey 指定的公钥的十六进制编码的低序 64 位哈希值。PublicKeyToken 是由 SetPublicKey 指定的十六进制编码的公钥。

十六进制编码的定义是,按照从最低有效字节到最高有效字节的顺序,将二进制对象的每个字节转换为两个十六进制字符。必要时添加附加的显示值。

如果已知完整的公钥,则可以用 PublicKey 替换 StrongName。

还要注意,除了必须先出现的 Name 以外,参数的文本顺序并不重要。但是,任何未明确设置的参数(VersionCultureStrongNamePublicKey)将被视为可以忽略,AssemblyName 则被视为不完整。指定部分信息时,必须按以上顺序指定 Name 参数。

当提供显示名称时,约定 StrongName =nullPublicKey= null 指示需要针对简单名称的程序集的绑定和匹配。另外,约定 Culture= ""(双引号表示空字符串)指示针对默认区域性的匹配。

下面的代码示例显示带有默认区域性且名称简单的程序集的 AssemblyName

com.microsoft.crypto,
    Culture=""

下面的代码示例显示区域性为“en”且带有强名称的程序集的完全限定引用。

com.microsoft.crypto, Culture=en, PublicKeyToken=a5d015c7d5a0b012, Version=1.0.0.0

示例

本示例演示如何使用各种反射类来分析包含在程序集中的元数据。

Imports System
Imports System.Reflection

Module Module1

    Sub Main()
        ' This variable holds the amount of indenting that 
        ' should be used when displaying each line of information.
        Dim indent As Int32 = 0
        ' Display information about the EXE assembly.
        Dim a As [Assembly] = System.Reflection.Assembly.GetExecutingAssembly()
        Display(indent, "Assembly identity={0}", a.FullName)
        Display(indent + 1, "Codebase={0}", a.CodeBase)

        ' Display the set of assemblies our assemblies references.
        Dim an As AssemblyName
        Display(indent, "Referenced assemblies:")
        For Each an In a.GetReferencedAssemblies()
            Display(indent + 1, "Name={0}, Version={1}, Culture={2}, PublicKey token={3}", _
                an.Name, an.Version, an.CultureInfo.Name, BitConverter.ToString(an.GetPublicKeyToken))
        Next
        Display(indent, "")

        ' Display information about each assembly loading into this AppDomain.
        For Each a In AppDomain.CurrentDomain.GetAssemblies()
            Display(indent, "Assembly: {0}", a)

            ' Display information about each module of this assembly.
            Dim m As [Module]
            For Each m In a.GetModules(True)
                Display(indent + 1, "Module: {0}", m.Name)
            Next

            ' Display information about each type exported from this assembly.
            Dim t As Type
            indent += 1
            For Each t In a.GetExportedTypes()
                Display(0, "")
                Display(indent, "Type: {0}", t)

                ' For each type, show its members & their custom attributes.
                Dim mi As MemberInfo
                indent += 1
                For Each mi In t.GetMembers()
                    Display(indent, "Member: {0}", mi.Name)
                    DisplayAttributes(indent, mi)

                    ' If the member is a method, display information about its parameters.
                    Dim pi As ParameterInfo
                    If mi.MemberType = MemberTypes.Method Then
                        For Each pi In CType(mi, MethodInfo).GetParameters()
                            Display(indent + 1, "Parameter: Type={0}, Name={1}", pi.ParameterType, pi.Name)
                        Next
                    End If

                    ' If the member is a property, display information about the property's accessor methods.
                    If mi.MemberType = MemberTypes.Property Then
                        Dim am As MethodInfo
                        For Each am In CType(mi, PropertyInfo).GetAccessors()
                            Display(indent + 1, "Accessor method: {0}", am)
                        Next
                    End If
                Next
                indent -= 1
            Next
            indent -= 1
        Next
    End Sub

    ' Displays the custom attributes applied to the specified member.
    Sub DisplayAttributes(ByVal indent As Int32, ByVal mi As MemberInfo)
        ' Get the set of custom attributes; if none exist, just return.
        Dim attrs() As Object = mi.GetCustomAttributes(False)
        If attrs.Length = 0 Then Return

        ' Display the custom attributes applied to this member.
        Display(indent + 1, "Attributes:")
        Dim o As Object
        For Each o In attrs
            Display(indent + 2, "{0}", o.ToString())
        Next
    End Sub

    ' Display a formatted string indented by the specified amount.
    Sub Display(ByVal indent As Int32, ByVal format As String, ByVal ParamArray params() As Object)
        Console.Write(New String(" "c, indent * 2))
        Console.WriteLine(format, params)
    End Sub
End Module

'The output shown below is abbreviated.
'
'Assembly identity=Reflection, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
'  Codebase=file:///C:/Reflection.exe
'Referenced assemblies:
'  Name=mscorlib, Version=1.0.5000.0, Culture=, PublicKey token=B7-7A-5C-56-19-34-E0-89
'  Name=Microsoft.VisualBasic, Version=7.0.5000.0, Culture=, PublicKey token=B0-3F-5F-7F-11-D5-0A-3A
'
'Assembly: mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
'  Module: mscorlib.dll
'  Module: prc.nlp
'  Module: prcp.nlp
'  Module: ksc.nlp
'  Module: ctype.nlp
'  Module: xjis.nlp
'  Module: bopomofo.nlp
'  Module: culture.nlp
'  Module: region.nlp
'  Module: sortkey.nlp
'  Module: charinfo.nlp
'  Module: big5.nlp
'  Module: sorttbls.nlp
'  Module: l_intl.nlp
'  Module: l_except.nlp
'
'  Type: System.Object
'    Member: GetHashCode
'    Member: Equals
'      Parameter: Type=System.Object, Name=obj
'    Member: ToString
'    Member: Equals
'      Parameter: Type=System.Object, Name=objA
'      Parameter: Type=System.Object, Name=objB
'    Member: ReferenceEquals
'      Parameter: Type=System.Object, Name=objA
'      Parameter: Type=System.Object, Name=objB
'    Member: GetType
'    Member: .ctor
'
'  Type: System.ICloneable
'    Member: Clone
'
'  Type: System.Collections.IEnumerable
'    Member: GetEnumerator
'      Attributes:
'        System.Runtime.InteropServices.DispIdAttribute
'
'  Type: System.Collections.ICollection
'    Member: get_IsSynchronized
'    Member: get_SyncRoot
'    Member: get_Count
'    Member: CopyTo
'      Parameter: Type=System.Array, Name=array
'      Parameter: Type=System.Int32, Name=index
'    Member: Count
'      Accessor method: Int32 get_Count()
'    Member: SyncRoot
'      Accessor method: System.Object get_SyncRoot()
'    Member: IsSynchronized
'      Accessor method: Boolean get_IsSynchronized()
'

继承层次结构

System.Object
  System.Reflection.AssemblyName

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

AssemblyName 成员
System.Reflection 命名空间
Version
GetPublicKey
GetPublicKeyToken
SetPublicKey
SetPublicKeyToken
FullName
GetType
AssemblyQualifiedName

其他资源

指定完全限定的类型名称