Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Assembly.GetCallingAssembly Method

Returns the Assembly of the method that invoked the currently executing method.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

public static Assembly GetCallingAssembly()

Return Value

Type: System.Reflection.Assembly
The Assembly object of the method that invoked the currently executing method.

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:

  • Method M1 in assembly A1 calls GetCallingAssembly.

  • Method M2 in assembly A2 calls M1.

  • Method M3 in assembly A3 calls M2.

When M1 is not inlined, GetCallingAssembly returns A2. When M1 is inlined, GetCallingAssembly returns A3. Similarly, when M2 is not inlined, GetCallingAssembly returns A2. When M2 is inlined, GetCallingAssembly returns A3.

This effect also occurs when M1 executes as a tail call from M2, or when M2 executes as a tail call from M3. 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.

The following example gets the calling assembly of the current method.

using System;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      // Instantiate a target object. 
      int 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 = " + Assembly.GetCallingAssembly().FullName);
   }
}
// The example displays output like the following: 
//    GetCallingAssembly = Example, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft