Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Get Namespace Name [modified]

Get Namespace Name [modified]

Scheduled Pinned Locked Moved C#
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DaveyM69
    wrote on last edited by
    #1

    I'm trying to get the namespace that a class resides in within a static method of that class. this.GetType().Namespace; works if the method is not static but obviously this is not valid in a static method. Edit: I've found that this way works but I'm sure there's a better way!

    private class dummy
    { }

    static string GetNamespace()
    {
    dummy temp = new dummy();
    return temp.GetType().Namespace;
    }

    Dave

    modified on Monday, March 3, 2008 7:52 AM

    T P 2 Replies Last reply
    0
    • D DaveyM69

      I'm trying to get the namespace that a class resides in within a static method of that class. this.GetType().Namespace; works if the method is not static but obviously this is not valid in a static method. Edit: I've found that this way works but I'm sure there's a better way!

      private class dummy
      { }

      static string GetNamespace()
      {
      dummy temp = new dummy();
      return temp.GetType().Namespace;
      }

      Dave

      modified on Monday, March 3, 2008 7:52 AM

      T Offline
      T Offline
      TJoe
      wrote on last edited by
      #2

      You can simply use typeof(MyClass).Namespace.

      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

      D 2 Replies Last reply
      0
      • T TJoe

        You can simply use typeof(MyClass).Namespace.

        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        Thanks Tom, it works and will solve my problem but I was looking for a more general way if there is one. The static method must already be in a class, so surely it should know what MyClass is without me having to specify it?

        Dave

        1 Reply Last reply
        0
        • D DaveyM69

          I'm trying to get the namespace that a class resides in within a static method of that class. this.GetType().Namespace; works if the method is not static but obviously this is not valid in a static method. Edit: I've found that this way works but I'm sure there's a better way!

          private class dummy
          { }

          static string GetNamespace()
          {
          dummy temp = new dummy();
          return temp.GetType().Namespace;
          }

          Dave

          modified on Monday, March 3, 2008 7:52 AM

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          You could always do this with the StackFrame. Here's a quick sample:

          using System;
          using System.Collections.Generic;
          using System.Text;
          using System.Diagnostics;

          namespace TestApp
          {
          class Program
          {
          static void Main(string[] args)
          {
          StackFrame sf = new StackFrame();

            Console.WriteLine(sf.GetMethod().DeclaringType.Namespace);
          }
          

          }
          }

          GetMethod has a property called DeclaringType which returns the type of the class the method is implemented in, and from there it's trivial to find the namespace.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          D 1 Reply Last reply
          0
          • T TJoe

            You can simply use typeof(MyClass).Namespace.

            Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            This works! System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace;

            Dave

            1 Reply Last reply
            0
            • P Pete OHanlon

              You could always do this with the StackFrame. Here's a quick sample:

              using System;
              using System.Collections.Generic;
              using System.Text;
              using System.Diagnostics;

              namespace TestApp
              {
              class Program
              {
              static void Main(string[] args)
              {
              StackFrame sf = new StackFrame();

                Console.WriteLine(sf.GetMethod().DeclaringType.Namespace);
              }
              

              }
              }

              GetMethod has a property called DeclaringType which returns the type of the class the method is implemented in, and from there it's trivial to find the namespace.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              Cheers Pete, I actually found and posted the solution at the same time as you! I used System.Reflection.MethodBase.GetCurrentMethod() instead of StackFrame.GetMethd() Are there any advantages of either one?

              Dave

              P 1 Reply Last reply
              0
              • D DaveyM69

                Cheers Pete, I actually found and posted the solution at the same time as you! I used System.Reflection.MethodBase.GetCurrentMethod() instead of StackFrame.GetMethd() Are there any advantages of either one?

                Dave

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                DaveyM69 wrote:

                Cheers Pete, I actually found and posted the solution at the same time as you! I used System.Reflection.MethodBase.GetCurrentMethod() instead of StackFrame.GetMethd() Are there any advantages of either one?

                Well, the StackFrame always implicitly returns MethodBase information, plus it returns line and file information if it's a debug build (obviously no line/file info should be returned for a release build). So, no real benefit of either one.

                Deja View - the feeling that you've seen this post before.

                My blog | My articles

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups