0
我聲明瞭以下類。我需要檢索類結構和靜態值,而不需要實例化它。GetValue嵌套類中的靜態字段
public class MyClass()
{
public static string field = "Value";
public class nestedClass()
{
public static string nestedField = "NestedValue";
}
}
我successfuly使用GetFields和GetNestedType恢復類結構和getValue(NULL)正常工作現場,但不能在nestedField。 讓我示例:
var fi = typeof(MyClass).GetField("field", BindingFlags.Public | BindingFlags.Static);
var nt = typeof(MyClass).GetNestedType("nestedClass", BindingFlags.Public);
var nfi = nt.GetField("nestedField", BindingFlags.Public | BindingFlags.Static);
// All the above references are detected correctly
var value = fi.GetValue(null); // until here everything works fine. value == "Value"
var nestedValue = nfi.GetValue(null); // this one does not work!!
任何人都知道爲什麼最後一行不工作,如何解決? 謝謝。
請提供一段簡短但完整的代碼段。目前,您沒有嵌套類 - 您有一個名爲'nestedClass'的*方法*。如果您可以向我們提供我們可以剪切,粘貼和運行的內容,這將對您有很大的幫助。 – 2010-04-07 13:04:58
這將是值得知道你得到什麼異常。例如,它可能是代碼安全問題。 – Robert 2011-03-14 14:18:39