Assembly.GetCallingAssembly Assembly.GetCallingAssembly Assembly.GetCallingAssembly Assembly.GetCallingAssembly Method

定义

返回方法(该方法调用当前正在执行的方法)的 AssemblyReturns the Assembly of the method that invoked the currently executing method.

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 对象。The Assembly object of the method that invoked the currently executing method.

示例

下面的示例获取当前方法的调用程序集。The following example gets the calling assembly of the current method.

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()
        {
            FistMethod();
            SecondAssembly.InSecondAssembly.OtherMethod();
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void FirstMethod()
        {
            Console.WriteLine("FirstMethod called from: " + Assembly.GetCallingAssembly().FullName);
        }
    }
}

// Assembly SecondAssembly
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace SecondAssembly
{
    class InSecondAssembly
    {
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void OtherMethod()
        {
            Console.WriteLine("OtherMehod 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可能会发生异常。If the method that calls the GetCallingAssembly method is expanded inline by the just-in-time (JIT) compiler, or if its caller is expanded inline, the assembly that is returned by GetCallingAssembly may differ unexpectedly. 例如,考虑以下方法和程序集:For example, consider the following methods and assemblies:

  • 方法M1程序集中A1调用GetCallingAssemblyMethod M1 in assembly A1 calls GetCallingAssembly.

  • 方法M2程序集中A2调用M1Method M2 in assembly A2 calls M1.

  • 方法M3程序集中A3调用M2Method M3 in assembly A3 calls M2.

M1不是内联GetCallingAssembly返回A2When M1 is not inlined, GetCallingAssembly returns A2. M1内联,GetCallingAssembly返回A3When M1 is inlined, GetCallingAssembly returns A3. 同样,当M2不是内联GetCallingAssembly返回A2Similarly, when M2 is not inlined, GetCallingAssembly returns A2. M2内联,GetCallingAssembly返回A3When M2 is inlined, GetCallingAssembly returns A3.

这种效果也会发生时M1尾调用从作为执行M2,或当M2尾调用从作为执行M3This effect also occurs when M1 executes as a tail call from M2, or when M2 executes as a tail call from M3. 您可能会阻止从 JIT 编译器内联调用的方法GetCallingAssembly,通过应用MethodImplAttribute属性与MethodImplOptions.NoInlining标志,但没有任何类似机制来防止尾调用。You can prevent the JIT compiler from inlining the method that calls GetCallingAssembly, by applying the MethodImplAttribute attribute with the MethodImplOptions.NoInlining flag, but there is no similar mechanism for preventing tail calls.

适用于