Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if property is generic List<of T> via Reflection and loop list items

I'm looping all the properties in an object via reflection:

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

Can anyone tell me how to determine whether the property in question is a generic List(Of T)? If it is I need to loop the list itself.

I've experimented with GetType and TypeOf but have not managed to get anything working.

Thanks.

****Update and clarification**

To clarify, I want to keep this generic. I do not want to specify the type of T, I need to loop the list items and call the ToString method on each item. T could be one of a number of different types (application specific reference types). Is it possible to do this without specifying types?

(VB.NET 2005 with .Net 2.0)

like image 338
Simon Avatar asked Oct 01 '09 13:10

Simon


2 Answers

Try this complete console application. Sorry it's 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();
        }


    }           
}
like image 65
Roatin Marth Avatar answered Oct 30 '22 07:10

Roatin Marth


Here is Roatins answer in VB.Net, Complete 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
like image 39
Ryu Avatar answered Oct 30 '22 08:10

Ryu