Assembly.GetCallingAssembly Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
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
Valori restituiti
Oggetto Assembly del metodo che ha richiamato il metodo attualmente in esecuzione.
Eccezioni
In .NET 11 e versioni successive, il supporto di analisi dello stack è disabilitato.
Esempio
Nell'esempio seguente viene restituito l'assembly chiamante del metodo corrente.
// 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 GetCallingAssembly metodo viene espanso inline dal compilatore JIT (Just-In-Time) o se il chiamante viene espanso inline, l'assembly restituito da GetCallingAssembly può variare in modo imprevisto. Si considerino ad esempio i metodi e gli assembly seguenti:
Il metodo
M1nell'assemblyA1chiama GetCallingAssembly.Il metodo
M2nell'assemblyA2chiamaM1.Il metodo
M3nell'assemblyA3chiamaM2.
Quando M1 non è inlined, GetCallingAssembly restituisce A2. Quando M1 è inlined, restituisce GetCallingAssemblyA3. Analogamente, quando M2 non è inlined, GetCallingAssembly restituisce A2. Quando M2 è inlined, restituisce GetCallingAssemblyA3.
Questo effetto si verifica anche quando M1 viene eseguito come chiamata finale da M2o quando M2 viene eseguito come chiamata finale da M3. È possibile impedire al compilatore JIT di inlining il metodo che chiama GetCallingAssembly, applicando l'attributo MethodImplAttribute con il MethodImplOptions.NoInlining flag , ma non esiste un meccanismo simile per impedire le chiamate di coda.
A partire da .NET 11, questo metodo supporta AOT nativo e usa i dati di analisi dello stack per identificare il chiamante. Se l'opzione di StackTraceSupport funzionalità è impostata su false, il metodo genera NotSupportedException.