Assembly.GetCallingAssembly Metodo

Definizione

Restituisce l'oggetto Assembly del metodo che ha richiamato il metodo attualmente in esecuzione.

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

Restituisce

Oggetto Assembly del metodo che ha richiamato il metodo attualmente in esecuzione.

Esempio

Nell'esempio seguente viene restituito l'assembly chiamante del metodo corrente.

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

Commenti

Se il metodo che chiama il metodo viene espanso inline dal compilatore JIT (Just-In-Time) o se il GetCallingAssembly chiamante viene espanso inline, l'assembly restituito da GetCallingAssembly può variare in modo imprevisto. Si considerino ad esempio i metodi e gli assembly seguenti:

  • Metodo M1 nelle chiamate GetCallingAssemblydi assembly A1 .

  • Metodo M2 nelle chiamate M1di assembly A2 .

  • Metodo M3 nelle chiamate M2di assembly A3 .

Quando M1 non è inlined, GetCallingAssembly restituisce A2. Quando M1 è inlined, GetCallingAssembly restituisce A3. Analogamente, quando M2 non è inlined, GetCallingAssembly restituisce A2. Quando M2 è inlined, GetCallingAssembly restituisce A3.

Questo effetto si verifica anche quando viene eseguito come chiamata di coda da M2o quando M2M1 viene eseguito come chiamata di coda da M3. È possibile impedire al compilatore JIT di inlining il metodo che chiama GetCallingAssembly, applicando l'attributo con il MethodImplOptions.NoInliningMethodImplAttribute flag, ma non esiste alcun meccanismo simile per impedire le chiamate di coda.

Si applica a