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 do método que invocou o método atualmente em execução.
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 objeto Assembly do método que invocou o método atualmente em execução.
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 retornado por GetCallingAssembly poderá diferir inesperadamente. Por exemplo, considere os seguintes métodos e assemblies:
O método
M1no assemblyA1chama GetCallingAssembly.O método
M2no assemblyA2chamaM1.O método
M3no assemblyA3chamaM2.
Quando M1 não está embutido, GetCallingAssembly retorna A2. Quando M1 está embutido, GetCallingAssembly retorna A3. Da mesma forma, quando M2 não está embutido, GetCallingAssembly retorna A2. Quando M2 está embutido, GetCallingAssembly retorna A3.
Esse efeito também ocorre quando M1 é executado como uma chamada final de M2ou 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 final.