How can I retrieve the namespace to a string C#

后端 未结 9 970
一向
一向 2020-12-15 02:38

I am writing a program which needs the namespace of the program but I cant seem to figure out how to retrieve it. I would like the end result to be in a string.

I wa

相关标签:
9条回答
  • 2020-12-15 03:03

    You could simply use typeof and then pass in the class (I.e. Program):

    Console.WriteLine(typeof(Program).Namespace); 
    

    Which would print:

    ConsoleApplication1
    
    0 讨论(0)
  • 2020-12-15 03:03

    If you're executing it from a class in the namespace you need to capture then you can just use:

    GetType().Namespace

    This works nicely as it then allows you to refactor the namespace and will still work.

    0 讨论(0)
  • 2020-12-15 03:06

    This can't go wrong:

    MethodBase.GetCurrentMethod().DeclaringType.Namespace
    
    0 讨论(0)
提交回复
热议问题