Assembly.GetExecutingAssembly Metodo

Definizione

Ottiene l'assembly che contiene il codice attualmente in esecuzione.

public static System.Reflection.Assembly GetExecutingAssembly();

Restituisce

Assembly che contiene il codice attualmente in esecuzione.

Esempio

Nell'esempio seguente viene utilizzata la Type.Assembly proprietà per ottenere l'assembly attualmente in esecuzione in base a un tipo contenuto nell'assembly. Chiama anche il GetExecutingAssembly metodo per mostrare che restituisce un Assembly oggetto che rappresenta lo stesso assembly.

using System;
using System.Reflection;

class Example
{
   static void Main()
   {
      // Get the assembly from a known type in that assembly.
      Type t = typeof(Example);
      Assembly assemFromType = t.Assembly;
      Console.WriteLine("Assembly that contains Example:");
      Console.WriteLine("   {0}\n", assemFromType.FullName);

      // Get the currently executing assembly.
      Assembly currentAssem = Assembly.GetExecutingAssembly();
      Console.WriteLine("Currently executing assembly:");
      Console.WriteLine("   {0}\n", currentAssem.FullName);

      Console.WriteLine("The two Assembly objects are equal: {0}",
                        assemFromType.Equals(currentAssem));
   }
}
// The example displays the following output:
//    Assembly that contains Example:
//       GetExecutingAssembly1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
//
//    Currently executing assembly:
//       GetExecutingAssembly1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
//
//    The two Assembly objects are equal: True

Commenti

Per motivi di prestazioni, è consigliabile chiamare questo metodo solo quando non si conosce in fase di progettazione quale assembly è attualmente in esecuzione. Il modo consigliato per recuperare un Assembly oggetto che rappresenta l'assembly corrente consiste nell'utilizzare la Type.Assembly proprietà di un tipo trovato nell'assembly, come illustrato nell'esempio seguente.

using System;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      Assembly assem = typeof(Example).Assembly;
      Console.WriteLine("Assembly name: {0}", assem.FullName);
   }
}
// The example displays output like the following:
//    Assembly name: Assembly1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

Per ottenere l'assembly che contiene il metodo che ha chiamato il codice attualmente in esecuzione, usare GetCallingAssembly.

Si applica a

Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1