How can I retrieve the namespace to a string C#

后端 未结 9 1009
一向
一向 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 02:58

    This should work:

    var myType = typeof(MyClass);
    var n = myType.Namespace;
    

    Write out to the console:

    Type myType = typeof(MyClass);
    Console.WriteLine("Namespace: {0}.", myType.Namespace);
    

    Setting a WinForm label:

    Type myType = typeof(MyClass);
    namespaceLabel.Text = myType.Namespace;
    

    Or create a method in the relevant class and use anywhere:

    public string GetThisNamespace()
    {
       return GetType().Namespace;
    }
    

提交回复
热议问题