Assembly.GetCallingAssembly Yöntem

Tanım

Assembly Şu anda yürütülmekte olan yöntemi çağıran yöntemin değerini döndürür.

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

Döndürülenler

Şu Assembly anda yürütülmekte olan yöntemi çağıran yöntemin nesnesi.

Örnekler

Aşağıdaki örnek geçerli yöntemin çağıran derlemesini alır.

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

Açıklamalar

yöntemini çağıran GetCallingAssembly yöntem tam zamanında (JIT) derleyicisi tarafından satır içi olarak genişletilirse veya çağıranı satır içinde genişletilirse, tarafından GetCallingAssembly döndürülen derleme beklenmedik şekilde farklılık gösterebilir. Örneğin, aşağıdaki yöntemleri ve derlemeleri göz önünde bulundurun:

  • Derleme A1GetCallingAssemblyçağrılarındaki yöntemiM1.

  • Derleme A2M1çağrılarındaki yöntemiM2.

  • Derleme A3M2çağrılarındaki yöntemiM3.

M1 Inlined olmadığında döndürür GetCallingAssemblyA2. Inlined olduğunda M1 döndürür GetCallingAssemblyA3. Benzer şekilde, M2 inlined olmadığında döndürür GetCallingAssemblyA2. Inlined olduğunda M2 döndürür GetCallingAssemblyA3.

Bu etki, 'den bir kuyruk çağrısı olarak yürütürse veya 'den M2M3bir kuyruk çağrısı olarak yürütürse M2 de oluşurM1. özniteliğini bayrağıyla uygulayarak JIT derleyicisinin çağıran GetCallingAssemblyyönteminin içini çizmesini MethodImplAttributeMethodImplOptions.NoInlining engelleyebilirsiniz, ancak kuyruk çağrılarını önlemeye yönelik benzer bir mekanizma yoktur.

Şunlara uygulanır