Assembly.GetCallingAssembly Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
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 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.
// 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 wywołująca metodę GetCallingAssembly jest rozszerzana w tekście przez kompilator just in time (JIT) lub jeśli jego obiekt wywołujący jest rozszerzany w tekście, zestaw zwracany przez GetCallingAssembly program może się nieoczekiwanie różnić. Rozważmy na przykład następujące metody i zestawy:
Metoda w wywołaniu zestawu
A1wywołuje GetCallingAssemblymetodęM1.Metoda w wywołaniu zestawu
A2wywołujeM1metodęM2.Metoda w wywołaniu zestawu
A3wywołujeM2metodęM3.
Gdy M1 element nie jest podkreślony, GetCallingAssembly zwraca wartość A2. Gdy M1 jest podkreślony, GetCallingAssembly zwraca wartość A3. Podobnie, gdy M2 element nie jest podkreślony, GetCallingAssembly zwraca wartość A2. Gdy M2 jest podkreślony, GetCallingAssembly zwraca wartość A3.
Ten efekt występuje również wtedy, gdy M1 jest wykonywane jako wywołanie ogonowe z M2elementu lub, gdy M2 jest wykonywane jako wywołanie ogona z M3. Kompilator JIT może uniemożliwić tworzenie metody wywołującej metodę GetCallingAssembly, stosując MethodImplAttribute atrybut z flagą MethodImplOptions.NoInlining , ale nie ma podobnego mechanizmu zapobiegania wywołaniom ogonowym.