2009-10-01 30 views
8

Tôi lặp tất cả các thuộc tính trong một đối tượng thông qua phản ánh:Xác định nếu tài sản là generic Danh sách <of T> qua Reflection và danh sách loop mục

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

bất cứ ai có thể cho tôi biết làm thế nào để xác định xem có bất động sản là một Danh sách chung (Of ​​T)? Nếu tôi cần phải lặp lại chính danh sách đó.

Tôi đã thử nghiệm với GetType và TypeOf nhưng chưa quản lý được bất kỳ thứ gì hoạt động.

Cảm ơn.

**** Cập nhật và làm rõ **

Để làm rõ, tôi muốn giữ điều này. Tôi không muốn chỉ định kiểu T, tôi cần lặp lại các mục danh sách và gọi phương thức ToString trên mỗi mục. T có thể là một trong số các loại khác nhau (các kiểu tham chiếu cụ thể của ứng dụng). Có thể thực hiện điều này mà không chỉ định loại không?

(VB.NET 2005 với Net 2,0)

Trả lời

4

Dưới đây là câu trả lời Roatins trong VB.Net, ứng dụng điều khiển Hoàn

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

Uhhh, không phải là điều này bất hợp pháp để làm trên stackoverflow ?? –

+1

Chỉ cần cố gắng để giúp anh chàng ra ngoài. Tôi đã sử dụng converter.telerik.com – Ryu

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

Chỉnh sửa: Nếu TypeOf p.PropertyType Is List (Of T) Sau đó ... – Simon

13

Hãy thử ứng dụng giao diện điều khiển hoàn chỉnh này. Xin lỗi nó trong 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

Ở đây bạn đi trong VB.NET. (Tôi sử dụng .NET 4.5). Nếu đối tượng ban đầu của bạn là Danh sách (T) với variable name = MyData, sau đó

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

Trên đang cung cấp cho tất cả các thuộc tính bên trong MyData List.

Lặp qua các thuộc tính trong danh sách chính (MyData) và muốn tìm xem có bất kỳ thuộc tính đơn nào là loại danh sách hay không, sử dụng dưới đây cho vòng lặp. Bạn có thể loại bỏ IsGenericType kiểm tra nếu không yêu cầu dựa trên yêu cầu của bạn.

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 
Các vấn đề liên quan