bairog
bairog

Reputation: 3373

How to get an assembly containing interface without Assembly.GetCallingAssembly()

I have a C# WinForms application and two assemblies involved - first contains an interface, second contains class which implements that inteface.

Application loads second assembly (via MS Enterprise Library Unity - i. e. via Reflection) and calls class methods.

I need to log that fact and before I was using the following code

var ApplicationInfo = Assembly.GetEntryAssembly().ToString();
var InterfaceInfo = Assembly.GetCallingAssembly().ToString();
var ImplementationInfo = Assembly.GetExecutingAssembly().ToString();

It was working correctly some time ago (it obtained application, first assembly and second assembly respectively).

But now for some reason GetCallingAssembly() and GetExecutingAssembly() both return second assembly (containing class inplementation).

Is there a way to get an assembly containing interface without Assembly.GetCallingAssembly()?

Any idea why GetCallingAssembly() started to be equal GetExecutingAssembly()?

Upvotes: 0

Views: 234

Answers (1)

Richard
Richard

Reputation: 109015

If you have a type in the current assembly implementing the interface then the definition has to be loaded. With a type definition you can get an assembly reference:

var asm = typeof(IInterface).Assembly;

If you have an instance (eg. created dynamically) then:

var asm = theObject.GetType().Assembly;

Upvotes: 2

Related Questions