I have the following situation:
Assembly A– Class library, contains my controlControl A.Control Ahas propertyResourceName.
Assembly B– Winforms app, contains branding resources (images, strings, etc.).Assembly BreferencesAssembly Aand uses it’s custom controls.Solution– contains project files of assembliesAandB.
Control A locates the resource ResourceName using GetEntryAssembly() to obtain an assembly reference, which works at runtime, but at designtime I guess the entry assembly is devenv.exe or something similar so it doesn’t work.
Ideally I should be able to load the assembly which created (instantiated) the control.
I could even introduce a new Control A property AssemblyName, but I’d need full path in order to load it, and I can’t get to the path of Assembly B from code at designtime.
EDIT: Thanks to Richard I’m now using Assembly.GetCallingAssemby() in the control’s constructor and caching the value in a private field, which is more universal than GetEntryAssembly.Unfortunatly it still doesn’t work at designtime (I get System.dll as a calling assembly). I suspect that this is because VS designer doesn’t instantiate the whole class but only the .designer.cs part.
It is easy to get the assembly that implements a type (
System.Typehas anAssemblyproperty). But the only time you can get the caller of a method is in that method.So you could get the caller of the constructor’s assembly while executing the constructor.
But with the exception of capturing it with IntelliTrace (if you have VS Ultimate), you cannot go back in time to get that stack trace later.