Assembly.GetCallingAssembly Metoda

Definice

Assembly Vrátí metodu, která vyvolala aktuálně spuštěnou metodu.

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

Návraty

Objekt Assembly metody, která vyvolala aktuálně spuštěnou metodu.

Příklady

Následující příklad získá volání sestavení aktuální 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

Poznámky

Pokud je metoda, která volá metodu GetCallingAssembly , rozšířena kompilátorem JIT (just-in-time) nebo pokud je jeho volající vložený, sestavení, které je vráceno GetCallingAssembly , se může neočekávaně lišit. Představte si například následující metody a sestavení:

  • Metoda M1 ve volání A1sestavení GetCallingAssembly .

  • Metoda M2 ve volání A2sestavení M1 .

  • Metoda M3 ve volání A3sestavení M2 .

Pokud M1 není vložen, GetCallingAssembly vrátí .A2 Když M1 je vložen, GetCallingAssembly vrátí .A3 Podobně, pokud M2 není vložen, GetCallingAssembly vrátí A2. Když M2 je vložen, GetCallingAssembly vrátí .A3

K tomuto efektu dochází také při M1 provádění jako koncové volání z M2, nebo při M2 provádění jako koncové volání z M3. Kompilátoru JIT můžete zabránit v vložení metody, která volá GetCallingAssembly, použitím atributu MethodImplAttribute s příznakem MethodImplOptions.NoInlining , ale neexistuje žádný podobný mechanismus pro zabránění koncovým voláním.

Platí pro