2009-10-01 64 views
8

我通過反射循環對象中的所有屬性泛型列表 確定是否屬性是通過反射和循環列表項

For Each p As PropertyInfo In values.[GetType]().GetProperties() 
    If p.CanRead Then 
     'Do stuff 
    End If 
Next 

誰能告訴我如何判斷是否有問題的屬性是通用List(Of T)?如果是我需要循環列表本身。

我已經嘗試過使用GetType和TypeOf,但還沒有設法使任何工作。

謝謝。

****更新和澄清**

爲了澄清,我想保持這種通用的。我不想指定T的類型,我需要循環列表項並在每個項目上調用ToString方法。 T可能是許多不同類型之一(應用程序特定的參考類型)。沒有指定類型可以這樣做嗎?

(VB.NET 2005 .NET 2.0)

回答

4

這裏是Roatins答案在VB.Net,完整的控制檯應用程序

Imports System 
Imports System.Reflection 
Imports System.Collections.Generic 
Imports System.Collections 

Namespace ReflectionTest 
    Public Class Object1 
     Public Overloads Overrides Function ToString() As String 
      Return "This is Object 1" 
     End Function 
    End Class 
    Public Class Object2 
     Public Overloads Overrides Function ToString() As String 
      Return "This is Object 2" 
     End Function 
    End Class 

    Public Class ContainerClass 
     Public Property objects() As List(Of Object) 
      Get 
      End Get 
      Set 
      End Set 
     End Property 
     Public Property propA() As Integer 
      Get 
      End Get 
      Set 
      End Set 
     End Property 
     Public Property propB() As String 
      Get 
      End Get 
      Set 
      End Set 
     End Property 
     Public Property propC() As String() 
      Get 
      End Get 
      Set 
      End Set 
     End Property 
    End Class 
    Class Program 
     Shared Sub Main(args As String()) 
      ' Sample class instance 
      Dim c As New ContainerClass() 

      ' Add some sample data 
      c.objects = New List(Of Object)() 
      c.objects.Add(New Object1()) 
      c.objects.Add(New Object2()) 

      Dim props As PropertyInfo() = c.[GetType]().GetProperties() 

      For Each p As PropertyInfo In props 
       If GetType(IList).IsAssignableFrom(p.PropertyType) AndAlso p.PropertyType.IsGenericType Then 
        Dim item As IList = DirectCast(p.GetValue(c, Nothing), IList) 
        If item <> Nothing Then 
         For Each o As Object In item 
          Console.WriteLine(o.ToString()) 
         Next 
        End If 
       End If 
      Next 
      Console.ReadLine() 
     End Sub 


    End Class 
End Namespace 
+0

唔,是不是這種非法做的計算器? – 2009-10-01 16:28:34

+1

只是試圖幫助這個傢伙。我使用converter.telerik.com – Ryu 2009-10-01 16:41:58

-1
if p.PropertyType = TypeOf List(Of T) then... 
+0

更正:如果TypeOf運算p.PropertyType爲list(中T)則... – Simon 2009-10-01 14:40:43

13

試試這個完整的控制檯應用程序。對不起,它在C#中。

using System; 
using System.Reflection; 
using System.Collections.Generic; 
using System.Collections; 

namespace ReflectionTest 
{ 
    public class Object1 
    { 
     public override string ToString() 
     { 
      return "This is Object 1"; 
     } 
    } 
    public class Object2 
    { 
     public override string ToString() 
     { 
      return "This is Object 2"; 
     } 
    }  

    public class ContainerClass 
    { 
     public List<object> objects { get; set; } 
     public int propA { get; set; } 
     public string propB { get; set; } 
     public string[] propC { get; set; } 
    } 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Sample class instance 
      ContainerClass c = new ContainerClass(); 

      // Add some sample data 
      c.objects = new List<object>(); 
      c.objects.Add(new Object1()); 
      c.objects.Add(new Object2()); 

      PropertyInfo[] props = c.GetType().GetProperties(); 

      foreach (PropertyInfo p in props) 
      { 
       if (typeof(IList).IsAssignableFrom(p.PropertyType) 
        && p.PropertyType.IsGenericType) 
       { 
        IList item = (IList)p.GetValue(c, null); 
        if (item != null) 
        { 
         foreach (object o in item) 
         { 
          Console.WriteLine(o.ToString()); 
         } 
        } 
       } 
      } 
      Console.ReadLine(); 
     } 


    }   
} 
0

在這裏,您在VB.NET去。 (我使用.NET 4.5)。 如果你的原始對象是列表(的T)與變量名=邁德特,然後

Dim CurCols() as PropertyInfo = MyData.GetType.GetGenericArguments()(0).GetProperties 

以上代碼給MyData的列表中的所有屬性。

循環遍歷主列表(MyData)中的屬性,並希望查找是否有任何單個屬性本身是列表類型,請在下面使用for循環。根據您的要求,您可以刪除IsGenericType檢查是否需要。

For Each iCol In CurCols 
    Dim colType as Type = iCol.PropertyType 
    If colType.IsGenericType AndAlso colType.GetGenericTypeDefinition = GetType(List(Of)) Then 
     MsgBox(iCol.Name.ToString & " Is a List Type.") 
    End If 
Next 
相關問題