Assembly.GetCallingAssembly Método

Definición

Devuelve el Assembly del método al que llamó el método actualmente en ejecución.

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

Devoluciones

Assembly

Objeto Assembly del método al que llamó el método actualmente en ejecución.

Ejemplos

En el ejemplo siguiente se obtiene el ensamblado que realiza la llamada del método actual.

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

Comentarios

Si el compilador Just-In-Time (JIT) expande en línea el método que llama GetCallingAssembly al método, o si su llamador se expande en línea, el ensamblado devuelto por GetCallingAssembly puede diferir inesperadamente. Por ejemplo, considere los métodos y ensamblados siguientes:

  • El método M1 del ensamblado A1 llama a GetCallingAssembly.

  • El método M2 del ensamblado A2 llama a M1.

  • El método M3 del ensamblado A3 llama a M2.

Cuando M1 no está insertado, GetCallingAssembly devuelve A2. Cuando M1 está insertado, GetCallingAssembly devuelve A3. De forma similar, cuando M2 no está insertado, GetCallingAssembly devuelve A2. Cuando M2 está insertado, GetCallingAssembly devuelve A3.

Este efecto también se produce cuando M1 se ejecuta como una llamada de cola desde M2o cuando M2 se ejecuta como una llamada de cola desde M3. Puede evitar que el compilador JIT inserte el método que llama GetCallingAssemblya , aplicando el MethodImplAttribute atributo con la MethodImplOptions.NoInlining marca , pero no hay ningún mecanismo similar para evitar las llamadas finales.

Se aplica a