Dot-Net

在靜態函式中獲取命名空間

  • September 9, 2015

在實例方法中,我可以輕鬆找到正在執行的命名空間:

public void PrintNamespace()
{
 Console.WriteLine(this.GetType().Namespace);
}

問:如何在不明確提及類名的情況下在靜態函式中做同樣的事情(沒有這個可用)?(沒有typeof(MyClass)

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

或使用反射:

Console.WriteLine(MethodBase.GetCurrentMethod().DeclaringType.Namespace);

引用自:https://stackoverflow.com/questions/1831480