2009-10-01 9 views
8

sto loop tutte le proprietà di un oggetto attraverso la riflessione:Determinare se la proprietà è elenco generico <of T> tramite elementi di riflessione e di elenco ciclo

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

Qualcuno può dirmi come determinare se la proprietà in questione è un Elenco generico (Of T)? Se è necessario ho il ciclo della lista stessa.

Ho sperimentato con GetType e TypeOf ma non sono riuscito a far funzionare nulla.

Grazie.

**** Update e chiarimenti **

Per chiarire, voglio mantenere questo generico. Non voglio specificare il tipo di T, ho bisogno di collegare gli elementi della lista e chiamare il metodo ToString su ciascun elemento. T potrebbe essere uno di un numero di tipi diversi (tipi di riferimento specifici dell'applicazione). È possibile farlo senza specificare i tipi?

(VB.NET 2005 Net 2,0)

risposta

4

Ecco Roatins risposta in VB.Net, completa applicazione console

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, non è questo illegale da fare su StackOverflow ?? –

+1

Solo cercando di aiutare il ragazzo. Ho usato converter.telerik.com – Ryu

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

Correzione: Se TypeOf p.PropertyType è List (Of T) Quindi ... – Simon

13

Prova questa applicazione console completa. Scusa, è in 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

Qui si va in VB.NET. (Io uso .NET 4.5). Se l'oggetto originale è List (T) con il nome variabile = MyData, quindi

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

Soprattutto codice dà tutte le proprietà all'interno MyData List.

Fare un ciclo tra le proprietà nell'elenco principale (MyData) e si desidera trovare se una singola proprietà è di per sé un tipo di elenco, utilizzare in basso per il ciclo. È possibile rimuovere il controllo IsGenericType se non richiesto in base alle proprie esigenze.

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 
Problemi correlati