Assembly.GetCallingAssembly Assembly.GetCallingAssembly Assembly.GetCallingAssembly Assembly.GetCallingAssembly Method

정의

현재 실행 중인 메서드를 호출한 메서드의 Assembly를 반환합니다.Returns the Assembly of the method that invoked the currently executing method.

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 개체입니다.The Assembly object of the method that invoked the currently executing method.

예제

다음 예에서는 현재 메서드의 호출 어셈블리를 가져옵니다.The following example gets the calling assembly of the current method.

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()
        {
            FistMethod();
            SecondAssembly.InSecondAssembly.OtherMethod();
        }

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

// Assembly SecondAssembly
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace SecondAssembly
{
    class InSecondAssembly
    {
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void OtherMethod()
        {
            Console.WriteLine("OtherMehod 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 메서드는 시간 (JIT) 컴파일러에 의해 확장 된 인라인 또는 해당 호출자에 게 확장 된 인라인 인 경우 어셈블리는 반환한 GetCallingAssembly 예기치 않게 다를 수 있습니다.If the method that calls the GetCallingAssembly method is expanded inline by the just-in-time (JIT) compiler, or if its caller is expanded inline, the assembly that is returned by GetCallingAssembly may differ unexpectedly. 예를 들어 다음 메서드 및 어셈블리를 고려 합니다.For example, consider the following methods and assemblies:

  • 메서드 M1 어셈블리에서 A1 호출 GetCallingAssembly합니다.Method M1 in assembly A1 calls GetCallingAssembly.

  • 메서드 M2 어셈블리에서 A2 호출 M1합니다.Method M2 in assembly A2 calls M1.

  • 메서드 M3 어셈블리에서 A3 호출 M2합니다.Method M3 in assembly A3 calls M2.

M1 인라인 화 되지 GetCallingAssembly 반환 A2합니다.When M1 is not inlined, GetCallingAssembly returns A2. M1 인라인 GetCallingAssembly 반환 A3합니다.When M1 is inlined, GetCallingAssembly returns A3. 마찬가지로, M2 인라인 화 되지 GetCallingAssembly 반환 A2합니다.Similarly, when M2 is not inlined, GetCallingAssembly returns A2. M2 인라인 GetCallingAssembly 반환 A3합니다.When M2 is inlined, GetCallingAssembly returns A3.

이 효과도 발생 하면 M1 마무리 호출에서 실행 되 M2, 때나 M2 마무리 호출에서 실행 되 M3합니다.This effect also occurs when M1 executes as a tail call from M2, or when M2 executes as a tail call from M3. JIT 컴파일러를 방지할 수 있습니다 인라인 호출 하는 메서드 GetCallingAssembly를 적용 하 여는 MethodImplAttribute 특성과 MethodImplOptions.NoInlining 플래그를 이지만 마무리 호출을 방지 하는 것에 대 한 유사한 메커니즘이 없습니다.You can prevent the JIT compiler from inlining the method that calls GetCallingAssembly, by applying the MethodImplAttribute attribute with the MethodImplOptions.NoInlining flag, but there is no similar mechanism for preventing tail calls.

적용 대상