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

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

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

適用於