Assembly.GetCallingAssembly Metoda

Definicja

Zwraca metodę Assembly , która wywołała aktualnie wykonującą metodę.

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

Zwraca

Assembly

Assembly Obiekt metody, która wywołała aktualnie wykonującą metodę.

Przykłady

Poniższy przykład pobiera zestaw wywołujący bieżącej metody.

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

Uwagi

Jeśli metoda, która wywołuje GetCallingAssembly metodę, jest rozwinięta w tekście przez kompilator just in time (JIT) lub jeśli jej obiekt wywołujący jest rozwinięty w tekście, zestaw zwracany przez GetCallingAssembly może się nieoczekiwanie różnić. Rozważmy na przykład następujące metody i zestawy:

  • Metoda w zestawie A1 wywołuje GetCallingAssemblymetodę M1 .

  • Metoda w zestawie A2 wywołuje M1metodę M2 .

  • Metoda w zestawie A3 wywołuje M2metodę M3 .

Gdy M1 element nie jest wbudowany, GetCallingAssembly zwraca wartość A2. Gdy M1 element jest wznawiany, GetCallingAssembly zwraca wartość A3. Podobnie, gdy M2 element nie jest podkreślony, GetCallingAssembly zwraca wartość A2. Gdy M2 element jest wznawiany, GetCallingAssembly zwraca wartość A3.

Ten efekt występuje również, gdy M1 jest wykonywane jako wywołanie końcowe z M2, lub gdy M2 jest wykonywane jako wywołanie końcowe z M3. Można uniemożliwić kompilatorowi JIT tworzenie wbudowanej metody wywołującej GetCallingAssemblymetodę , stosując MethodImplAttribute atrybut z flagą MethodImplOptions.NoInlining , ale nie ma podobnego mechanizmu zapobiegania wywołaniom tail.

Dotyczy