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 개체입니다.

예제

다음 예제에서는 현재 메서드의 호출 어셈블리를 가져옵니다.

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 하는 메서드가 JIT(Just-In-Time) 컴파일러에 의해 인라인으로 확장되거나 호출자가 인라인으로 확장된 경우 반환 GetCallingAssembly 되는 어셈블리가 예기치 않게 다를 수 있습니다. 예를 들어 다음 메서드 및 어셈블리를 고려합니다.

  • 어셈블리 A1 호출GetCallingAssembly의 메서드 M1 입니다.

  • 어셈블리 A2 호출M1의 메서드 M2 입니다.

  • 어셈블리 A3 호출M2의 메서드 M3 입니다.

M1 인라인되지 않으면 .를 GetCallingAssembly 반환합니다A2. M1 인라인이 지정되면 .를 GetCallingAssembly 반환합니다A3. 마찬가지로 인라인 GetCallingAssembly A2되지 않은 경우 M2 . M2 인라인이 지정되면 .를 GetCallingAssembly 반환합니다A3.

이 효과는 테일 호출로 실행되거나 테일 호출M2``M3로 실행되는 경우에도 M2 발생 M1 합니다. JIT 컴파일러가 플래그를 사용하여 특성을 MethodImplOptions.NoInlining 적용하여 MethodImplAttribute 호출GetCallingAssembly하는 메서드를 인라인화하지 못하도록 방지할 수 있지만 비상 호출을 방지하기 위한 유사한 메커니즘은 없습니다.

적용 대상