Assembly.GetCallingAssembly Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Devolve 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
Devoluções
O Assembly objeto do método que invocou o método atualmente em execução.
Exemplos
O exemplo seguinte obtém a assembleia de chamada do método 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
Observações
Se o método que chama o GetCallingAssembly método for expandido inline pelo compilador just-in-time (JIT), ou se o seu chamador for expandido inline, o assembly que GetCallingAssembly é devolvido pode diferir inesperadamente. Por exemplo, considere os seguintes métodos e montagens:
O método
M1em chamadas GetCallingAssemblyassemblyA1.O método
M2em chamadasM1assemblyA2.O método
M3em chamadasM2assemblyA3.
Quando M1 não está em linha, GetCallingAssembly retorna A2. Quando M1 está em linha, GetCallingAssembly devolve A3. De forma semelhante, quando M2 não está em linha, GetCallingAssembly devolve A2. Quando M2 está em linha, GetCallingAssembly devolve A3.
Este efeito também ocorre quando M1 é executado como uma chamada de cauda de M2, ou quando M2 é executada como chamada de cauda de M3. Pode impedir que o compilador JIT faça inlining no método que chama GetCallingAssembly, aplicando o MethodImplAttribute atributo com a MethodImplOptions.NoInlining flag, mas não existe um mecanismo semelhante para evitar chamadas de cauda.