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 a whole assembly or module.
You can set many of the attributes in 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 by using the following syntax:
<Assembly: Attribute1, Assembly: Attribute2..., Assembly: AttributeN>
You specify module-level attributes by using similar syntax:
<Module: Attribute1, Module: Attribute2..., Module: AttributeN>
You add 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 put 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 you reference it in code. You can set an assembly's version and culture using attributes. However, the name value 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 that contains the assembly manifest. The AssemblyFlagsAttribute attribute specifies whether multiple copies of the assembly can coexist.
The following table shows the identity attributes.
Attribute |
Purpose |
---|---|
Fully describes the identity of an assembly. |
|
Specifies the version of an assembly. |
|
Specifies which culture the assembly supports. |
|
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 |
---|---|
Defines a custom attribute that specifies a product name for an assembly manifest. |
|
Defines a custom attribute that specifies a trademark for an assembly manifest. |
|
Defines a custom attribute that specifies an informational version for an assembly manifest. |
|
Defines a custom attribute that specifies a company name for an assembly manifest. |
|
Defines a custom attribute that specifies a copyright for an assembly manifest. |
|
Instructs the compiler to use a specific version number for the Win32 file version resource. |
|
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. This includes title, description, default alias, and configuration. The following table shows the assembly manifest attributes defined in the System.Reflection namespace.
Attribute |
Purpose |
---|---|
Defines a custom attribute that specifies an assembly title for an assembly manifest. |
|
Defines a custom attribute that specifies an assembly description for an assembly manifest. |
|
Defines a custom attribute that specifies an assembly configuration (such as retail or debug) for an assembly manifest. |
|
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 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 |
---|---|
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. |
|
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")] |
|
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 the 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). When you sign an assembly with a strong name, it adds a public key encryption to the file that contains the assembly manifest. Strong-name signing helps 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
Use the Strong Name Tool (Sn.exe) to generate a key file that contains a key pair.
Add strong name attributes to the AssemblyInfo.vb file created automatically with Visual Basic projects. To edit this file, double-click the file name in the Solution Explorer.
The following example uses delay signing to create a strong-named assembly with a public key file that is named myKey.snk.
<Assembly: Reflection.AssemblyKeyFile("myKey.snk")> <Assembly: Reflection.AssemblyDelaySign(True)>
Note
Assembly-level attributes are ignored if you do not create an assembly, such as when you compile from the command line by using the /target:module option.
See Also
Tasks
How to: Define Your Own Attributes
How to: Sign an Assembly with a Strong Name
Concepts
Assembly Security Considerations
Reference
File Signing Tool (Signcode.exe)
Signing Page, Project Designer
Assembly Information Dialog Box
Other Resources
Custom Attributes in Visual Basic