Assembly.GetCallingAssembly メソッド

定義

現在実行中のメソッドを呼び出したメソッドの Assembly を返します。

public:
 static System::Reflection::Assembly ^ GetCallingAssembly();
public static System.Reflection.Assembly GetCallingAssembly ();
static member GetCallingAssembly : unit -> System.Reflection.Assembly
Public Shared Function GetCallingAssembly () As Assembly

戻り値

現在実行中のメソッドを呼び出したメソッドの Assembly オブジェクト。

次の例では、現在のメソッドの呼び出し元アセンブリを取得します。

using namespace System;
using namespace System::Reflection;

void main()
{
   // Instantiate a target object.
   Int32 integer1 = 0;
   // Set the Type instance to the target class type.
   Type^ type1 = integer1.GetType();
   // Instantiate an Assembly class to the assembly housing the Integer type.
   Assembly^ sampleAssembly = Assembly::GetAssembly(integer1.GetType());
   // Display the name of the assembly that is calling the method.
   Console::WriteLine("GetCallingAssembly = {0}", Assembly::GetCallingAssembly()->FullName);
}
// The example displays output like the following:
//    GetCallingAssembly = Example, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// Assembly FirstAssembly
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace FirstAssembly
{
    public class InFirstAssembly
    {
        public static void Main()
        {
            FirstMethod();
            SecondAssembly.InSecondAssembly.OtherMethod();
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void FirstMethod()
        {
            Console.WriteLine("FirstMethod called from: " + Assembly.GetCallingAssembly().FullName);
        }
    }
}

// Assembly SecondAssembly
namespace SecondAssembly
{
    class InSecondAssembly
    {
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void OtherMethod()
        {
            Console.WriteLine("OtherMethod executing assembly: " + Assembly.GetExecutingAssembly().FullName);
            Console.WriteLine("OtherMethod called from: " + Assembly.GetCallingAssembly().FullName);
        }
    }
}
// The example produces output like the following:
// "FirstMethod called from: FirstAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
// "OtherMethod executing assembly: SecondAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
// "OtherMethod called from: FirstAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
Imports System.Reflection

Module Example
   Public Sub Main()
      ' Instantiate a target object.
      Dim int1 As Integer
      ' Set the Type instance to the target class type.
      Dim type1 As Type =int1.GetType()
      ' Instantiate an Assembly class to the assembly housing the Integer type.
      Dim sampleAssembly = Assembly.GetAssembly(int1.GetType())
      ' Display the name of the assembly that is calling the method.
      Console.WriteLine(("GetCallingAssembly = " + Assembly.GetCallingAssembly().FullName))
   End Sub
End Module
' The example displays output like the following:
'   GetCallingAssembly = Example, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

注釈

メソッドを呼び出す GetCallingAssembly メソッドが Just-In-Time (JIT) コンパイラによってインライン展開されている場合、またはその呼び出し元がインラインで展開されている場合は、 によって GetCallingAssembly 返されるアセンブリが予期せず異なる場合があります。 たとえば、次のメソッドとアセンブリについて考えてみましょう。

  • アセンブリの メソッドM1は を呼び出しますGetCallingAssemblyA1

  • アセンブリの メソッドM2は を呼び出しますM1A2

  • アセンブリの メソッドM3は を呼び出しますM2A3

がインライン化されていない場合 M1GetCallingAssembly は を返します A2。 がインライン化されている場合 M1GetCallingAssembly は を返します A3。 同様に、 がインライン化されていない場合 M2 は を GetCallingAssembly 返します A2。 がインライン化されている場合 M2GetCallingAssembly は を返します A3

この効果は、 から末尾呼び出しとして実行される場合M1、または からM2M3末尾呼び出しとして実行される場合M2にも発生します。 JIT コンパイラが を呼び出すメソッドのインライン化を防ぐには、 フラグを使用して 属性MethodImplOptions.NoInliningMethodImplAttribute適用しますが、末尾の呼び出GetCallingAssemblyしを防ぐための同様のメカニズムはありません。

適用対象