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

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

我能够找到一个关于这个主题的 MSDN 页面,但它被证明对我自己没有帮助。 Http://msdn.microsoft.com/en-us/library/system.type.namespace.aspx

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

编辑: 对不起,伙计们,这不是一个控制台应用。

91965 次浏览

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

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

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

Which would print:

ConsoleApplication1

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

if you have item x of class A in namespace B you can use:

string s = x.GetType().Namespace;

no s contains "B"

you can also use x.GetType().Name to get the type name or x.GetType().FullName to get both

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.

This can't go wrong:

MethodBase.GetCurrentMethod().DeclaringType.Namespace

To add to all the answers.

Since C# 6.0 there is the nameof keyword.

string name = nameof(MyNamespace);

This has several advantages:

  1. The name is resolved at compile-time
  2. The name will change when refactoring the namespace
  3. It is syntax checked, so the name must exist
  4. cleaner code

Note: This doesn't give the full namespace though. In this case, name will be equal to Bar:

namespace Foo.Bar
{
string name = nameof(Foo.Bar);
}

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.

as a roll upp all post answers: getting all columns' values from a table given as a string 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...