Get Namespace Name [modified]
-
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 obviouslythis
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
-
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 obviouslythis
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
You can simply use typeof(MyClass).Namespace.
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
-
You can simply use typeof(MyClass).Namespace.
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
-
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 obviouslythis
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
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.
-
You can simply use typeof(MyClass).Namespace.
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
-
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.
-
Cheers Pete, I actually found and posted the solution at the same time as you! I used
System.Reflection.MethodBase.GetCurrentMethod()
instead ofStackFrame.GetMethd()
Are there any advantages of either one?Dave
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.