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

Definition

Gibt die Assembly der Methode zurück, die die derzeit ausgeführte Methode aufgerufen hat.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

Gibt zurück

Das Assembly-Objekt der Methode, die die derzeit ausgeführte Methode aufgerufen hat.The Assembly object of the method that invoked the currently executing method.

Beispiele

Im folgenden Beispiel wird die aufrufenden Assembly der aktuellen Methode abgerufen.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

Hinweise

Wenn die Methode, die die GetCallingAssembly -Methode aufruft, durch den Just-in-time (JIT)-Compiler Inline erweitert wird, oder wenn der Aufrufer Inline erweitert wird, GetCallingAssembly kann sich die von zurückgegebene Assembly unerwartet unterscheiden.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. Beachten Sie z. b. die folgenden Methoden und Assemblys:For example, consider the following methods and assemblies:

  • Methode M1 GetCallingAssemblyin assemblyaufrufen.A1Method M1 in assembly A1 calls GetCallingAssembly.

  • Methode M2 M1in assemblyaufrufen.A2Method M2 in assembly A2 calls M1.

  • Methode M3 M2in assemblyaufrufen.A3Method M3 in assembly A3 calls M2.

Wenn M1 nicht Inline ist, GetCallingAssembly wird zurück A2gegeben.When M1 is not inlined, GetCallingAssembly returns A2. Wenn M1 Inline ist, GetCallingAssembly wird zurück A3gegeben.When M1 is inlined, GetCallingAssembly returns A3. Wenn M2 nicht Inline ist, GetCallingAssembly wird ebenfalls zurück A2gegeben.Similarly, when M2 is not inlined, GetCallingAssembly returns A2. Wenn M2 Inline ist, GetCallingAssembly wird zurück A3gegeben.When M2 is inlined, GetCallingAssembly returns A3.

Dieser Effekt tritt auch auf M1 , wenn als aufrufende von M2oder M2 als Endaufruf von M3ausgeführt wird.This effect also occurs when M1 executes as a tail call from M2, or when M2 executes as a tail call from M3. Sie können verhindern, dass der JIT-Compiler die Methode aufruft, GetCallingAssemblydie aufruft, indem MethodImplAttribute Sie das- MethodImplOptions.NoInlining Attribut mit dem-Flag anwenden, aber es gibt keinen ähnlichen Mechanismus zum Verhindern von Tail-aufrufen.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.

Gilt für: