Alex Rivera | Logout

C# Getting Parent Assembly Name of Calling Assembly

Asked 2012-06-13T11:53:15.237
43

I've got a C# unit test application that I'm working on. There are three assemblies involved - the assembly of the C# app itself, a second assembly that the app uses, and a third assembly that's used by the second one.

So the calls go like this:

First Assembly ------> Second Assembly---------> Third Assembly.

What I need to do in the third assembly is get the name of the Fist Assembly that called the second assembly.

Assembly.GetExecutingAssembly().ManifestModule.Name
Assembly.GetCallingAssembly().ManifestModule.Name

returns the name of the Second assembly. and

Assembly.GetEntryAssembly().ManifestModule.Name

return NULL

Does anybody know if there is a way to get to the assembly name of the First Assembly?

As per the other users demand here I put the code. This is not 100% code but follow of code like this.

namespace FirstAssembly{
public static xcass A
{
        public static Stream OpenResource(string name)
        {
            return Reader.OpenResource(Assembly.GetCallingAssembly(), ".Resources." + name);
        }
}
}

using FirstAssembly;
namespace SecondAssembly{
public static class B 

{
public static Stream FileNameFromType(string Name)

{
return = A.OpenResource(string name);
}
}
}

and Test project method

using SecondAssembly;
namespace ThirdAssembly{
public class TestC
{

 [TestMethod()]
        public void StremSizTest()
        {
            // ARRANGE
            var Stream = B.FileNameFromType("ValidMetaData.xml");
            // ASSERT
            Assert.IsNotNull(Stream , "The Stream  object should not be null.");
        }
}
}
Edit
Report

2 Answers

7

Assembly.GetEntryAssembly() is null if you run tests from nunit-console too.

If you just want the name of the executing app then use:

 System.Diagnostics.Process.GetCurrentProcess().ProcessName 

or

 Environment.GetCommandLineArgs()[0];

For nunit-console you would get "nunit-console" and "C:\Program Files\NUnit 2.5.10\bin\net-2.0\nunit-console.exe" respectively.

answered 2012-06-22T05:05:15.873
-1

How about Assembly.GetEntryAssembly()? It returns the main executable of the process.

Process.GetCurrentProcess().MainModule.ModuleName should also return about the same as the ManifestModule name ("yourapp.exe").

answered 2012-06-13T11:56:13.800

Your Answer