c# 4.0 - C# Getting Parent Assembly Name of Calling Assembly -
i've got c# unit test application i'm working on. there 3 assemblies involved - assembly of c# app itself, second assembly app uses, , third assembly that's used second one.
so calls go this:
first assembly ------> second assembly---------> third assembly.
what need in third assembly name of fist assembly called second assembly.
assembly.getexecutingassembly().manifestmodule.name assembly.getcallingassembly().manifestmodule.name
returns name of second assembly. ,
assembly.getentryassembly().manifestmodule.name
return null
does know if there way assembly name of first assembly?
as per other users demand here put code. not 100% code follow of code this.
namespace firstassembly{ public static xcass { 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 null."); } } }
i guess should able 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 assembly contains first method started first started in current thread. if you're not in main thread can different entryassembly, if understand situation correctly should assembly looking for.
you can actual assembly instead of name this:
assembly initialassembly = (from f in frames select f.getmethod().reflectedtype.assembly ).distinct().last();
edit - of sep. 23rd, 2015
please, notice
getmethod().reflectedtype
can null, retrieving assemblyqualifiedname throw exception. example, that's interesting if 1 wants check vanilla c.tor dedicated orm (like linq2db, etc...) poco class.
Comments
Post a Comment