如何将命名空间检索到字符串 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18485469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I retrieve the namespace to a string C#
提问by Elliot Ames
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 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
我能够找到有关此主题的 MSDN 页面,但事实证明它对我没有帮助。 http://msdn.microsoft.com/en-us/library/system.type.namespace.aspx
Any help would be appreciated. The program is written in C#.
任何帮助,将不胜感激。该程序是用 C# 编写的。
EDIT: Sorry guys, this is not a console application.
编辑:抱歉,这不是控制台应用程序。
采纳答案by Joe Ratzer
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:
设置 WinForm 标签:
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;
}
回答by Darren
回答by IllidanS4 wants Monica back
Put this to your assembly:
把它放到你的程序集中:
public static string GetCurrentNamespace()
{
return System.Reflection.Assembly.GetExecutingAssembly().EntryPoint.DeclaringType.Namespace;
}
Or if you want this method to be in a library used by your program, write it like this:
或者,如果您希望此方法位于您的程序使用的库中,请按如下方式编写:
[System.Runtime.CompilerServices.MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCurrentNamespace()
{
return System.Reflection.Assembly.GetCallingAssembly().EntryPoint.DeclaringType.Namespace;
}
回答by No Idea For Name
if you have item x
of class A
in namespace B
you can use:
如果命名空间中有x
类项,则可以使用:A
B
string s = x.GetType().Namespace;
no s
contains "B"
没有s
包含“B”
you can also use x.GetType().Name
to get the type name or x.GetType().FullName
to get both
您还可以使用x.GetType().Name
来获取类型名称或x.GetType().FullName
同时获取
回答by BenM
Type myType = typeof(MyClass);
// Get the namespace of the myClass class.
Console.WriteLine("Namespace: {0}.", myType.Namespace);
Building on Joe's comment you can still use
以乔的评论为基础,您仍然可以使用
Type myType = typeof(MyClass);
// Get the namespace of the myClass class.
var namespaceName = myType.Namespace.ToString();
with namespaceName
being a variable to access the namespace name as a string value.
与namespaceName
被访问的命名空间名称作为一个字符串值的变量。
回答by balage
This can't go wrong:
这不会出错:
MethodBase.GetCurrentMethod().DeclaringType.Namespace
回答by Serve Laurijssen
To add to all the answers.
添加到所有答案。
Since C# 6.0 there is the nameof keyword.
从 C# 6.0 开始就有了 nameof 关键字。
string name = nameof(MyNamespace);
This has several advantages:
这有几个优点:
- The name is resolved at compile-time
- The name will change when refactoring the namespace
- It is syntax checked, so the name must exist
- cleaner code
- 名称在编译时解析
- 重构命名空间时名称会发生变化
- 它经过语法检查,因此名称必须存在
- 更干净的代码
回答by Elliot Arazi
If you're executing it from a class in the namespace you need to capture then you can just use:
如果您从需要捕获的命名空间中的类执行它,那么您可以使用:
GetType().Namespace
GetType().Namespace
This works nicely as it then allows you to refactor the namespace and will still work.
这很有效,因为它允许您重构命名空间并且仍然可以工作。
回答by Fatih üNAL
as a roll upp all post answers: getting all columns' values from a table given as a string tableName:
作为汇总所有帖子的答案:从以字符串 tableName 给出的表中获取所有列的值:
var tableName = "INVENTORY_PRICE"; var assembly = Assembly.GetExecutingAssembly(); var tip = typeof(Form3); var t = assembly.GetType(tip.Namespace + "." + tableName); if (t != null) { var foos = db.GetTable(t); foreach (var f in foos) { Console.WriteLine(f + ":"); foreach (var property in f.GetType().GetProperties()) if (property != null) { var pv = property.GetValue(f, null); Console.WriteLine(" " + property.Name + ":" + pv); } Console.WriteLine("------------------------------------------------"); } }
it is very easy if we use ado, this sample uses LINQ context...
如果我们使用 ado 很容易,这个示例使用 LINQ 上下文......