2009-10-01 12 views
8

mam typami pętli wszystkie właściwości w obiekcie za pomocą refleksji:Ustal, czy nieruchomość jest rodzajowy Lista <of T> poprzez refleksję i lista pętla przedmiotów

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

Czy ktoś może mi powiedzieć, jak określić, czy dany majątek jest ogólna lista (z T)? Jeśli to konieczne, muszę zapętlić samą listę.

Eksperymentowałem z GetType i TypeOf, ale nie udało mi się uzyskać niczego działającego.

Dzięki.

**** Update i wyjaśnienie **

celu wyjaśnienia, chcę zachować ten rodzajowy. Nie chcę określać typu T, muszę zapętlić elementy listy i wywołać metodę ToString dla każdego elementu. T może być jednym z wielu różnych typów (typy referencyjne specyficzne dla aplikacji). Czy można to zrobić bez określania typów?

(VB.NET 2005 z .NET 2.0)

Odpowiedz

4

Oto Roatins odpowiedź w VB.NET, pełna Console Application

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, nie jest to niezgodne z prawem do zrobienia na stackoverflow ?? –

+1

Po prostu próbuję pomóc temu facetowi. Użyłem converter.telerik.com – Ryu

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

Korekta: Jeśli typeof p.PropertyType Czy List (Of T) Więc ... – Simon

13

Spróbuj kompletną aplikację konsoli. Przepraszam, że jest w 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

Tutaj można przejść w VB.NET. (Używam .NET 4.5). Jeśli oryginalny obiekt jest List (T) o zmiennej name = MyData, następnie

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

Powyższy kod daje wszystkie właściwości wewnątrz Mydata listy.

Zapętlając właściwości na głównej liście (MyData) i chcę sprawdzić, czy jakakolwiek pojedyncza właściwość jest typem listy, użyj poniższej pętli. Możesz usunąć test IsGenericType, jeśli nie jest to wymagane, w zależności od wymagań.

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