Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Get Calling Assembly from StackTrace

0.00/5 (No votes)
1 Jul 2014 1  
This article introduces a handy way to get the calling Testing Assembly from a called Assembly

Introduction

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.

Details

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:

    [TestFixture]
    public class TestClass
    {
        [Test]
        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=1.0.0.0, Culture=neutral, PublicKeyToken=null
Assembly.GetEntryAssembly() returns: 
Assembly.GetCallingAssembly() returns: AssemblyFromStackTrace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
The calling Assembly is AssemblyFromStackTraceTest, Version=1.0.0.0, 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here