共用方式為


Global Attributes in Visual Basic 

Most attributes are attached to specific language elements such as classes or methods. However, some attributes are global—they can apply to an entire assembly or module.

You can set many of the attributes within the Visual Studio Integrated Development Environment (IDE) through the Assembly Information Dialog Box. For more information, see Managing Application Properties and Managing Assembly and Manifest Signing.

Assembly Attributes

You specify assembly-level attributes using the following syntax:

<Assembly: Attribute1, Assembly: Attribute2..., Assembly: AttributeN>

You specify module-level attributes using similar syntax:

<Module: Attribute1, Module: Attribute2..., Module: AttributeN>

You place global attributes in source code after any top-level directives, such as Option Explicit and Imports statements, but before any type or namespace declarations. Global attributes can appear in multiple source files in a project, but they are generally placed in the file AssemblyInfo.vb file created automatically with Visual Basic projects.

Assembly attributes are values that provide information about an assembly. They fall into the following categories:

  • Assembly identity attributes

  • Informational attributes

  • Assembly manifest attributes

  • Strong name attributes

Assembly Identity Attributes

Three attributes (with a strong name, if applicable) determine the identity of an assembly: name, version, and culture. These attributes form the full name of the assembly and are required when referencing it in code. You can set an assembly's version and culture using attributes. The name value, however, is set by the compiler, the Visual Studio IDE in the Assembly Information Dialog Box, or the Assembly Linker (Al.exe) when the assembly is created, based on the file containing the assembly manifest. The AssemblyFlagsAttribute attribute specifies whether multiple copies of the assembly can coexist.

The following table shows the identity attributes.

Attribute Purpose

AssemblyName

Fully describes an assembly's identity

AssemblyVersionAttribute

Specifies the version of an assembly

AssemblyCultureAttribute

Specifies which culture the assembly supports

AssemblyFlagsAttribute

Specifies whether an assembly supports side-by-side execution on the same computer, in the same process, or in the same application domain

The following code applies the version and culture attributes to an assembly:

'Set version number for assembly.
<Assembly: Reflection.AssemblyVersionAttribute("4.3.2.1")> 
'Set culture as German.
<Assembly: Reflection.AssemblyCultureAttribute("de")> 

Informational Attributes

You can use informational attributes to provide additional company or product information for an assembly. The following table shows the informational attributes defined in the System.Reflection namespace.

Attribute Purpose

AssemblyProductAttribute

Defines a custom attribute that specifies a product name for an assembly manifest

AssemblyTrademarkAttribute

Defines a custom attribute that specifies a trademark for an assembly manifest

AssemblyInformationalVersionAttribute

Defines a custom attribute that specifies an informational version for an assembly manifest

AssemblyCompanyAttribute

Defines a custom attribute that specifies a company name for an assembly manifest

AssemblyCopyrightAttribute

Defines a custom attribute that specifies a copyright for an assembly manifest

AssemblyFileVersionAttribute

Instructs the compiler to use a specific version number for the Win32 file version resource

CLSCompliantAttribute

Indicates whether the assembly is compliant with the Common Language Specification (CLS).

Assembly Manifest Attributes

You can use assembly manifest attributes to provide information in the assembly manifest, including title, description, default alias, and configuration. The following table shows the assembly manifest attributes defined in the System.Reflection namespace.

Attribute Purpose

AssemblyTitleAttribute

Defines a custom attribute that specifies an assembly title for an assembly manifest

AssemblyDescriptionAttribute

Defines a custom attribute that specifies an assembly description for an assembly manifest

AssemblyConfigurationAttribute

Defines a custom attribute that specifies an assembly configuration (such as retail or debug) for an assembly manifest

AssemblyDefaultAliasAttribute

Defines a friendly default alias for an assembly manifest

Strong Name Attributes

Strong names are unique identifiers that protect the identity and integrity of an assembly. You can sign an assembly from within the Visual Studio IDE through the Signing Page, Project Designer. For more information, see Managing Assembly and Manifest Signing.

Alternatively, you can use strong-name attributes to set a strong name for an assembly. The following table shows the strong-name attributes defined in the System.Reflection namespace.

Attribute Purpose

AssemblyDelaySignAttribute

Boolean that indicates whether you want to reserve space in the executable file for the strong name signature, but defer the actual signing until some later stage. For more information, see Delay Signing an Assembly.

AssemblyKeyFileAttribute

Indicates the file that contains a key. The location of the KeyFile should be relative to the project output directory, which is %Project Directory%\obj\<configuration>. For example, if your KeyFile is located in the project directory, you would specify the AssemblyKeyFile attribute as

[assembly: AssemblyKeyFile("..\\..\\mykey.snk")]

AssemblyKeyNameAttribute

Refers to a key that has been installed in the Crypto Service Provider (CSP) on your computer. You must specify a key in order for your file to be signed.

If the KeyFile and the KeyName values are both specified, the following processing occurs:

  • If the KeyName can be found in the CSP, that key is used.

  • If the KeyName does not exist and the KeyFile does exist, the key in the KeyFile is installed into the CSP and used.

  • For more information, see Assembly Security Considerations.

Signing Assemblies

You can sign an assembly in two different but complementary ways: with a strong name or with signing. You can sign using the Visual Studio IDE Signing Page, Project Designer or by using the File Signing Tool (Signcode.exe). Signing an assembly with a strong name adds a public key encryption to the file containing the assembly manifest. Strong-name signing helps to verify name uniqueness, prevent name spoofing, and provide callers with some identity when a reference is resolved. For more information, see Managing Assembly and Manifest Signing and How to: Sign an Assembly with a Strong Name.

To sign an assembly with a strong name

  1. Use the Strong Name Tool (Sn.exe) to generate a key file containing a key pair.

  2. Add strong name attributes to the AssemblyInfo.vb file created automatically with Visual Basic projects. You can edit this file by double-clicking its name in the Solution Explorer.

    The following example uses delay signing to create a strong-named assembly with a public key file called myKey.snk.

    <Assembly: Reflection.AssemblyKeyFile("myKey.snk")> 
    <Assembly: Reflection.AssemblyDelaySign(True)> 
    

    Note

    Assembly-level attributes are ignored if you are not creating an assembly, such as when compiling from the command line using the /target:module option.

See Also

Tasks

How to: Define Your Own Attributes
How to: Sign an Assembly with a Strong Name

Reference

Strong Name Tool (Sn.exe)
File Signing Tool (Signcode.exe)
Signing Page, Project Designer
Assembly Information Dialog Box

Concepts

Application of Attributes
Assembly Security Considerations

Other Resources

Custom Attributes in Visual Basic
Managing Application Properties
Managing Assembly and Manifest Signing