Click here to Skip to main content
12,749,392 members (36,775 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 30 Jun 2014

Get Calling Assembly from StackTrace

, 1 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This article introduces a handy way to get the calling Testing Assembly from a called Assembly


Recently, when I was developing a class library that is expected to be called by some other testing library that is based on NUnit, there is some codes implemented at a static constructor of the called library to parse the types of the testing library thus the called function needs to know the external calling assembly first.

However, the Assembly.GetCallingAssembly() would always return the called library itself because the static constructor is called automatically before the instantiation of the class, while the Assembly.GetEntryAssembly() would always return null that might due to the fact that the execution codes is launched by NUnit Framework. It seems that I need to either move the codes to the calling testing libary or change the logics behind it.

Fortunately, I noticed that the calling sequence is crystal clear in the "Call stack" window of the Visual Studio, so I tried to use the StackTrace and get the expected result. Though this approach is quite simple, it did cost me some time to find it when there is no such discussion on the Internet, so maybe someone else would save his/her time to figure it out after reading this article.


The codes to get the calling assembly is quite simple:

using System.Diagnostics;
using System.Reflection;

        private static Assembly callingAssemblyByStackTrace()
            Assembly thisAssembly = Assembly.GetExecutingAssembly();

            StackTrace stackTrace = new StackTrace();
            StackFrame[] frames = stackTrace.GetFrames();

            foreach (var stackFrame in frames)
                var ownerAssembly = stackFrame.GetMethod().DeclaringType.Assembly;
                if (ownerAssembly != thisAssembly)
                    return ownerAssembly;
            return thisAssembly;

This method was intentionally placed out of the static Constructor as shown below:

public class Class1
    private static Assembly callingAssemblyByStackTrace(){}

    static Class1()
        Console.WriteLine("Assembly.GetExecutingAssembly() returns: " + Assembly.GetExecutingAssembly());
        Console.WriteLine("Assembly.GetEntryAssembly() returns: " + Assembly.GetEntryAssembly());
        Console.WriteLine("Assembly.GetCallingAssembly() returns: " + Assembly.GetCallingAssembly());

        Assembly callingAssembly = callingAssemblyByStackTrace();
        Console.WriteLine("The calling Assembly is " + callingAssembly);

    public Class1(){}

To test its functionality, there is another project of class library type referring both the above assemby and NUnit as below:

public class TestClass
    public void Test1()
        Class1 class1 = new Class1();

So the Testing codes run by NUnit would instantiate a Class1 object, which would trigger the static Class1() and the later would call the callingAssemblyByStackTrace() to get output:

Assembly.GetExecutingAssembly() returns: AssemblyFromStackTrace, Version=, Culture=neutral, PublicKeyToken=null
Assembly.GetEntryAssembly() returns: 
Assembly.GetCallingAssembly() returns: AssemblyFromStackTrace, Version=, Culture=neutral, PublicKeyToken=null
The calling Assembly is AssemblyFromStackTraceTest, Version=, Culture=neutral, PublicKeyToken=null

The AssemblyFromStackTraceTest is exactly what I expected.


Using the code

There are two projects within the attached zip file, extract both to a folder. The AssemblyFromStackTraceTest needs reference to both the AssemblyFromStackTrace project and NUnit, run it as test would see the above output in the console.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Australia Australia
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionYou got my 5 but... Pin
Pragmateek3-Jul-14 0:15
professionalPragmateek3-Jul-14 0:15 
AnswerRe: You got my 5 but... Pin
WilliamCruisoring3-Jul-14 3:06
memberWilliamCruisoring3-Jul-14 3:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170215.1 | Last Updated 1 Jul 2014
Article Copyright 2014 by JIANGWilliam
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid