Assembly.GetCallingAssembly().GetType().Namespace

  • Thread starter Thread starter Kelmen Wong
  • Start date Start date
K

Kelmen Wong

Greeting,

I'm working on a class managing resources (satelite).

public CResMgr()
{
Assembly objClient = Assembly.GetCallingAssembly();
_objResMgr = new ResourceManager(objClient.GetType().Namespace + '.'
+ DefaultBaseName, objClient);

The result of objClient.GetType().Namespace is "System.Reflection",
not what I'm expecting.


Presently, I workaround by passing the caller's Type.

_objResMgr = new CResMgr(this.GetType());

public CResMgr(Type objCaller)
{
_objResMgr = new ResourceManager(objCaller.Namespace + '.' +
DefaultBaseName, objCaller.Assembly);

Is there a way to dynamically to retrieve the Namespace of the
calling obj?
 
Kelmen Wong said:
I'm working on a class managing resources (satelite).

public CResMgr()
{
Assembly objClient = Assembly.GetCallingAssembly();
_objResMgr = new ResourceManager(objClient.GetType().Namespace + '.'
+ DefaultBaseName, objClient);

The result of objClient.GetType().Namespace is "System.Reflection",
not what I'm expecting.

Why not? The Assembly type is in the System.Reflection namespace, and
you're calling GetType() on an instance of Assembly.
Presently, I workaround by passing the caller's Type.

_objResMgr = new CResMgr(this.GetType());

public CResMgr(Type objCaller)
{
_objResMgr = new ResourceManager(objCaller.Namespace + '.' +
DefaultBaseName, objCaller.Assembly);

Is there a way to dynamically to retrieve the Namespace of the
calling obj?

I don't believe so.
 
Hi,

As it turns out there is a way to find out what called your assembly. It
seems to work in both Debug and Release.

First of all, do a stack trace.

C#
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(true);

then, to find out what called your code, just check the stack.. The first
item in the stack will be the function with the above line of code in it, the
second item in the stack will be the Method that called your code..

So, to find out the type, use the line below:

C#
trace.GetFrame(1).GetMethod().DeclaringType

As I said, this seems to work well in most situations.

Have fun..

Eddie de Bear
 
Hello Eddie,

Thanks for tip. I do aware about the StackTrace object, but not
going to work over it, due to a known limitation:

StackTrace might not report as many method calls as expected, due to
code transformations that occur during optimization.

The above statement is taken from the MSDN StackTrace object
remarks.

Presently, I'm adopting passing the Type from the caller to it.
 
Back
Top