Assembly.GetCallingAssembly().GetType().Namespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kelmen Wong

    #1

    Assembly.GetCallingAssembly().GetType().Namespace

    Greeting,

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

    public CResMgr()
    {
    Assembly objClient = Assembly.GetCal lingAssembly();
    _objResMgr = new ResourceManager (objClient.GetT ype().Namespace + '.'
    + DefaultBaseName , objClient);

    The result of objClient.GetTy pe().Namespace is "System.Reflect ion",
    not what I'm expecting.


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

    _objResMgr = new CResMgr(this.Ge tType());

    public CResMgr(Type objCaller)
    {
    _objResMgr = new ResourceManager (objCaller.Name space + '.' +
    DefaultBaseName , objCaller.Assem bly);

    Is there a way to dynamically to retrieve the Namespace of the
    calling obj?
  • Jon Skeet [C# MVP]

    #2
    Re: Assembly.GetCal lingAssembly(). GetType().Names pace

    Kelmen Wong <kelmen@hotmail .com> wrote:[color=blue]
    > I'm working on a class managing resources (satelite).
    >
    > public CResMgr()
    > {
    > Assembly objClient = Assembly.GetCal lingAssembly();
    > _objResMgr = new ResourceManager (objClient.GetT ype().Namespace + '.'
    > + DefaultBaseName , objClient);
    >
    > The result of objClient.GetTy pe().Namespace is "System.Reflect ion",
    > not what I'm expecting.[/color]

    Why not? The Assembly type is in the System.Reflecti on namespace, and
    you're calling GetType() on an instance of Assembly.
    [color=blue]
    > Presently, I workaround by passing the caller's Type.
    >
    > _objResMgr = new CResMgr(this.Ge tType());
    >
    > public CResMgr(Type objCaller)
    > {
    > _objResMgr = new ResourceManager (objCaller.Name space + '.' +
    > DefaultBaseName , objCaller.Assem bly);
    >
    > Is there a way to dynamically to retrieve the Namespace of the
    > calling obj?[/color]

    I don't believe so.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Eddie de Bear

      #3
      RE: Assembly.GetCal lingAssembly(). GetType().Names pace

      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.Diagnost ics.StackTrace trace = new System.Diagnost ics.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:
      [color=blue]
      > Greeting,
      >
      > I'm working on a class managing resources (satelite).
      >
      > public CResMgr()
      > {
      > Assembly objClient = Assembly.GetCal lingAssembly();
      > _objResMgr = new ResourceManager (objClient.GetT ype().Namespace + '.'
      > + DefaultBaseName , objClient);
      >
      > The result of objClient.GetTy pe().Namespace is "System.Reflect ion",
      > not what I'm expecting.
      >
      >
      > Presently, I workaround by passing the caller's Type.
      >
      > _objResMgr = new CResMgr(this.Ge tType());
      >
      > public CResMgr(Type objCaller)
      > {
      > _objResMgr = new ResourceManager (objCaller.Name space + '.' +
      > DefaultBaseName , objCaller.Assem bly);
      >
      > Is there a way to dynamically to retrieve the Namespace of the
      > calling obj?
      >[/color]

      Comment

      • Kelmen Wong

        #4
        Re: Assembly.GetCal lingAssembly(). GetType().Names pace

        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" <EddiedeBear@di scussions.micro soft.com> wrote in message news:<B9C92861-396C-405A-B323-212A18CC9EB0@mi crosoft.com>...[color=blue]
        > 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.Diagnost ics.StackTrace trace = new System.Diagnost ics.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.[/color]

        Comment

        Working...