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 方法的方法由实时 (JIT) 编译器内联展开,或者如果调用方以内联方式展开,则 返回 GetCallingAssembly 的 程序集可能会意外不同。 例如,请考虑以下方法和程序集:

  • 程序集A1中的 方法M1调用 GetCallingAssembly

  • 程序集A2中的 方法M2调用 M1

  • 程序集A3中的 方法M3调用 M2

如果未 M1 内联, GetCallingAssemblyA2返回 。 当 M1 内联时, GetCallingAssembly 返回 A3。 同样,如果未 M2 内联, GetCallingAssemblyA2返回 。 当 M2 内联时, GetCallingAssembly 返回 A3

当作为结尾调用从 执行时M1,或者当作为结尾调用从 M3执行时M2,也会发生此M2效果。 可以通过使用 标志应用 MethodImplAttribute 特性MethodImplOptions.NoInlining来阻止 JIT 编译器内联调用 GetCallingAssembly的方法,但不存在类似的机制来阻止尾部调用。

适用于