如何将命名空间检索到字符串 C#

4

本文介绍了如何将命名空间检索到字符串 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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14