Assembly.GetCallingAssembly Méthode

Définition

Retourne le Assembly de la méthode ayant appelé la méthode en cours d'exécution.

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

Retours

Objet Assembly de la méthode ayant appelé la méthode en cours d'exécution.

Exemples

L’exemple suivant obtient l’assembly appelant de la méthode actuelle.

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

Remarques

Si la méthode qui appelle la GetCallingAssembly méthode est développée inline par le compilateur juste-à-temps (JIT), ou si son appelant est développé inline, l’assembly retourné par GetCallingAssembly peut différer de manière inattendue. Par exemple, considérez les méthodes et assemblys suivants :

  • La méthode M1 dans l’assembly A1 appelle GetCallingAssembly.

  • La méthode M2 dans l’assembly A2 appelle M1.

  • La méthode M3 dans l’assembly A3 appelle M2.

Quand M1 n’est pas inclus, GetCallingAssembly retourne A2. Quand M1 est inlined, GetCallingAssembly retourne A3. De même, quand M2 n’est pas inclus, GetCallingAssembly retourne A2. Quand M2 est inlined, GetCallingAssembly retourne A3.

Cet effet se produit également lorsque M1 s’exécute en tant qu’appel de queue à partir de M2, ou quand s’exécute M2 en tant qu’appel de queue à partir de M3. Vous pouvez empêcher le compilateur JIT d’inliner la méthode qui appelle GetCallingAssembly, en appliquant l’attribut MethodImplAttribute avec l’indicateur MethodImplOptions.NoInlining , mais il n’existe aucun mécanisme similaire pour empêcher les appels de queue.

S’applique à