PC Review


Reply
Thread Tools Rate Thread

Assembly.GetCallingAssembly().GetType().Namespace

 
 
Kelmen Wong
Guest
Posts: n/a
 
      20th Oct 2004
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?
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      20th Oct 2004
Kelmen Wong <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?RWRkaWUgZGUgQmVhcg==?=
Guest
Posts: n/a
 
      21st Oct 2004
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

"Kelmen Wong" wrote:

> 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?
>

 
Reply With Quote
 
Kelmen Wong
Guest
Posts: n/a
 
      26th Oct 2004
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.

"Eddie de Bear" <(E-Mail Removed)> wrote in message news:<B9C92861-396C-405A-B323-(E-Mail Removed)>...
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't get Assembly.GetCallingAssembly() to reflect the proper assembly Brad Wood Microsoft Dot NET Framework 1 30th Nov 2006 04:56 PM
ERROR CS0234: The type or namespace name 'DataAccessHelper' does not exist in the namespace 'BCC' (are you missing an assembly reference?) li.eddie@gmail.com Microsoft ASP .NET 0 6th Jan 2006 11:31 AM
BUG???- Reflection.assembly.GetCallingAssembly returns different values when compiled for Debug or Release Shawn Hogan Microsoft Dot NET Framework 4 29th Apr 2004 01:05 PM
BUG???- Reflection.assembly.GetCallingAssembly returns different values when compiled for Debug or Release Shawn Hogan Microsoft VB .NET 0 28th Apr 2004 06:55 PM
The type or namespace name 'Windows' does not exist in the class or namespace 'System' (are you missing an assembly reference?) =?Utf-8?B?UmF5?= Microsoft Dot NET 1 7th Apr 2004 10:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:12 PM.