2009-01-29 51 views
2

我有一個類,它看起來是這樣的:反思找到嵌套類?

public class Parent 
{ 
    public class Subclass 
    { 
    } 
} 

,並使用反射,我試圖找到

void main 
{ 
    Parent p = new Parent(); 
    Type t = p.GetType(); 
    Type s = t.GetNestedType("Subclass"); //s is not set 
} 

這並不工作,因爲顯然沒有嵌套子類類型。我怎樣才能找到子類的類型?我需要獲取s的原因是稍後調用.GetMethod(「someMethod」)。在其上調用(...)。

回答

3

我只是想同樣的事情,和它的工作對我來說:

public class ParentClass 
    { 
     public class NestedClass 
     { 

     } 
    } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Type t = typeof(ParentClass); 
      Type t2 = t.GetNestedType("NestedClass"); 
      MessageBox.Show(t2.ToString()); 
     } 

你確定NestedClass是公開的?

+0

啊,我在反思錯誤的父類。感謝您驗證代碼! – 2009-01-29 22:37:32