如何将命名空间检索到字符串 C# [英] How can I retrieve the namespace to a string C#

查看:15
本文介绍了如何将命名空间检索到字符串 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要程序名称空间的程序,但我似乎不知道如何检索它.我希望最终结果是一个字符串.

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.

我能够找到有关此主题的 MSDN 页面,但事实证明它对我自己没有帮助.http://msdn.microsoft.com/en-us/library/system.type.命名空间.aspx

I was able to find an MSDN page about this topic but it proved to be unhelpful to myself. http://msdn.microsoft.com/en-us/library/system.type.namespace.aspx

任何帮助将不胜感激.该程序是用 C# 编写的.

Any help would be appreciated. The program is written in C#.

对不起,这不是控制台应用程序.

Sorry guys, this is not a console application.

推荐答案

这应该可行:

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

写出到控制台:

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

设置 WinForm 标签:

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;
}

这篇关于如何将命名空间检索到字符串 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆