Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Getting Parent Assembly Name of Calling Assembly

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.");         } } } 
like image 989
Saroop Trivedi Avatar asked Jun 13 '12 11:06

Saroop Trivedi


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

I guess you should be able to do it like this:

using System.Diagnostics; using System.Linq;  ...  StackFrame[] frames = new StackTrace().GetFrames(); string initialAssembly = (from f in frames                            select f.GetMethod().ReflectedType.AssemblyQualifiedName                          ).Distinct().Last(); 

This will get you the Assembly which contains the first method which was started first started in the current thread. So if you're not in the main thread this can be different from the EntryAssembly, if I understand your situation correctly this should be the Assembly your looking for.

You can also get the actual Assembly instead of the name like this:

Assembly initialAssembly = (from f in frames                            select f.GetMethod().ReflectedType.Assembly                          ).Distinct().Last(); 

Edit - as of Sep. 23rd, 2015

Please, notice that

GetMethod().ReflectedType 

can be null, so retrieving its AssemblyQualifiedName could throw an exception. For example, that's interesting if one wants to check a vanilla c.tor dedicated only to an ORM (like linq2db, etc...) POCO class.

like image 69
AVee Avatar answered Oct 07 '22 18:10

AVee


This will return the the initial Assembly that references your currentAssembly.

var currentAssembly = Assembly.GetExecutingAssembly(); var callerAssemblies = new StackTrace().GetFrames()             .Select(x => x.GetMethod().ReflectedType.Assembly).Distinct()             .Where(x => x.GetReferencedAssemblies().Any(y => y.FullName == currentAssembly.FullName)); var initialAssembly = callerAssemblies.Last(); 
like image 34
realstrategos Avatar answered Oct 07 '22 18:10

realstrategos