Leggere in inglese

Condividi tramite


AssemblyName.Version Proprietà

Definizione

Ottiene o imposta il numero principale, secondario, di build e di revisione dell'assembly.

public Version Version { get; set; }
public Version? Version { get; set; }

Valore della proprietà

Oggetto che rappresenta il numero principale, secondario, di build e di revisione dell'assembly.

Esempio

Questa sezione contiene due esempi. Nel primo esempio viene illustrato come recuperare la versione dell'assembly attualmente in esecuzione. Nel secondo esempio viene illustrato come utilizzare la Version proprietà per specificare la versione dell'assembly quando si genera un assembly dinamico.

Esempio 1

Nell'esempio seguente vengono recuperati e visualizzati i numeri di versione dell'assembly attualmente in esecuzione e dell'assembly che contiene la String classe .

using System;
using System.Reflection;

[assembly:AssemblyVersion("1.1.0.0")]

class Example
{
    static void Main()
    {
        Console.WriteLine("The version of the currently executing assembly is: {0}",
            typeof(Example).Assembly.GetName().Version);

        Console.WriteLine("The version of mscorlib.dll is: {0}",
            typeof(String).Assembly.GetName().Version);
    }
}

/* This example produces output similar to the following:

The version of the currently executing assembly is: 1.1.0.0
The version of mscorlib.dll is: 2.0.0.0
 */

Esempio 2

L'esempio seguente genera un assembly dinamico e lo salva nella directory corrente. Quando viene creato l'assembly, la Version proprietà viene utilizzata per specificare le informazioni sulla versione per l'assembly.

using System;
using System.Reflection;
using System.Threading;
using System.Reflection.Emit;

public class AssemblyName_Constructor
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {
      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");

      // Get all the assemblies currently loaded in the application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'.
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
      {
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];
      }
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the assembly name\n");
         Console.WriteLine(myAssembly);
      }
   }
}

Commenti

Tutti i componenti della versione devono essere numeri interi maggiori o uguali a zero. I metadati limitano i componenti principali, secondari, di compilazione e di revisione per un assembly a un valore massimo pari UInt16.MaxValue a - 1. Se un componente supera questo valore, non viene generato alcun errore; Tuttavia, in un assembly dinamico, tale componente è zero.

Si applica a