Assembly.GetCallingAssembly Método
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Retorna o Assembly método que invocou o método em execução no momento.
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
Retornos
O Assembly objeto do método que invocou o método em execução no momento.
Exemplos
O exemplo a seguir obtém o assembly de chamada do método atual.
// 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
Comentários
Se o método que chama o GetCallingAssembly método for expandido embutido pelo compilador JIT (just-in-time) ou se o chamador for expandido embutido, o assembly que é retornado por GetCallingAssembly ele poderá diferir inesperadamente. Por exemplo, considere os seguintes métodos e assemblies:
Método
M1em chamadas GetCallingAssemblyde assemblyA1.Método
M2em chamadasM1de assemblyA2.Método
M3em chamadasM2de assemblyA3.
Quando M1 não estiver embutido, GetCallingAssembly retornará A2. Quando M1 estiver embutido, GetCallingAssembly retornará A3. Da mesma forma, quando M2 não está embutido, GetCallingAssembly retorna A2. Quando M2 estiver embutido, GetCallingAssembly retornará A3.
Esse efeito também ocorre quando M1 é executado como uma chamada final de M2, ou quando M2 é executado como uma chamada final de M3. Você pode impedir que o compilador JIT inline o método que chama GetCallingAssembly, aplicando o MethodImplAttribute atributo com o MethodImplOptions.NoInlining sinalizador, mas não há mecanismo semelhante para impedir chamadas de cauda.