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 オブジェクト。

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

// 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によって返されるアセンブリが予期せず異なる場合があります。 たとえば、次のメソッドとアセンブリについて考えてみましょう。

  • アセンブリA1呼び出しGetCallingAssembly内のメソッドM1

  • アセンブリA2呼び出しM1内のメソッドM2

  • アセンブリA3呼び出しM2内のメソッドM3

M1がインライン化されていない場合、GetCallingAssemblyA2を返します。 M1がインライン化されると、GetCallingAssemblyA3を返します。 同様に、 M2 がインライン化されていない場合、 GetCallingAssemblyA2を返します。 M2がインライン化されると、GetCallingAssemblyA3を返します。

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

適用対象