Assembly.GetCallingAssembly 方法

定義

傳回方法的 Assembly,其叫用目前執行的方法。

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

傳回

方法的 Assembly 物件,其叫用目前執行的方法。

範例

下列範例會取得目前方法的呼叫元件。

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

備註

如果呼叫 GetCallingAssembly 方法的方法會由 Just-In-Time (JIT) 編譯程式內嵌展開,或呼叫端內嵌展開,則 傳 GetCallingAssembly 回的元件可能會意外不同。 例如,請考慮下列方法和元件:

  • 元件中的 方法M1會呼叫 GetCallingAssemblyA1

  • 元件中的 方法M2會呼叫 M1A2

  • 元件中的 方法M3會呼叫 M2A3

未內嵌時 M1GetCallingAssemblyA2回 。 內嵌時 M1GetCallingAssemblyA3回 。 同樣地,當未內嵌時 M2GetCallingAssembly 會傳 A2回 。 內嵌時 M2GetCallingAssemblyA3回 。

當 以的M2尾端呼叫執行,M1或以 的M3結尾呼叫的形式執行時M2,也會發生這個效果。 您可以使用 旗標套用 MethodImplAttribute 屬性MethodImplOptions.NoInlining,以防止 JIT 編譯程式內嵌呼叫 GetCallingAssembly的方法,但沒有類似的機制可防止尾端呼叫。

適用於