英語で読む

次の方法で共有


Assembly.GetExecutingAssembly メソッド

定義

現在実行中のコードを格納しているアセンブリを取得します。

C#
public static System.Reflection.Assembly GetExecutingAssembly();

戻り値

現在実行中のコードを格納しているアセンブリ。

次の例では、 プロパティを Type.Assembly 使用して、そのアセンブリに含まれる型に基づいて現在実行中のアセンブリを取得します。 また、 メソッドを GetExecutingAssembly 呼び出して、同じアセンブリを Assembly 表すオブジェクトが返されることを示します。

C#
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

注釈

パフォーマンス上の理由から、このメソッドは、デザイン時に現在実行されているアセンブリがわからない場合にのみ呼び出す必要があります。 次の例に示すように、現在のアセンブリを表すオブジェクトを取得 Assembly するには、アセンブリ内にある型の プロパティを使用 Type.Assembly することをお勧めします。

C#
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

現在実行中のコードを呼び出したメソッドを含むアセンブリを取得するには、 を使用 GetCallingAssemblyします。

適用対象

製品 バージョン
.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