PC Review


Reply
Thread Tools Rate Thread

C# Reflection List<T>

 
 
nobody knows
Guest
Posts: n/a
 
      7th May 2011
Hi, after searching the Web and trying and searching I dont figure it out.

I have an object of myClass which contains several List<T> objects which I want to reflect:

public myClass {
public List<foo> foos;
public List<bar> bars;
public other members;
}

Here is one of my tries to reflect the mainobject with using the MyReflection method:

private static void MyReflection(object obj)
{
var type = obj.GetType();

PropertyInfo[] list = type.GetProperties();
foreach (var item in list)
{
System.Diagnostics.Debug.WriteLine(item.Name);

var smi = type.GetProperty(item.Name);
var value = smi.GetValue(obj, null);
if (value.GetType().Name == "List`1")
MyReflection(value);

}
}


The point is, "value" turns into a List<T> object type which I dont know how to iterate through.

Somebody else knows it?

Thanks.


 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      7th May 2011
On 07-05-2011 11:09, nobody knows wrote:
> Hi, after searching the Web and trying and searching I dont figure it out.
>
> I have an object of myClass which contains several List<T> objects which I want to reflect:
>
> public myClass {
> public List<foo> foos;
> public List<bar> bars;
> public other members;
> }
>
> Here is one of my tries to reflect the mainobject with using the MyReflection method:
>
> private static void MyReflection(object obj)
> {
> var type = obj.GetType();
>
> PropertyInfo[] list = type.GetProperties();
> foreach (var item in list)
> {
> System.Diagnostics.Debug.WriteLine(item.Name);
>
> var smi = type.GetProperty(item.Name);
> var value = smi.GetValue(obj, null);
> if (value.GetType().Name == "List`1")
> MyReflection(value);
>
> }
> }
>
>
> The point is, "value" turns into a List<T> object type which I dont know how to iterate through.


I don't think you can do this type safe.

But it is easy to do not so type safe.

See below for inspiration.

Arne

PS: To be able to handle the classes within the list more sophisticated
than this, then you will need to do some recursion.


=====

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

namespace E
{
public class Foo
{
public override string ToString()
{
return "I am a Foo";
}
}
public class Bar
{
public override string ToString()
{
return "I am a Bar";
}
}
public class MyClass
{
public string Name { get; set; }
public List<Foo> Foos { get; set; }
public List<Bar> Bars { get; set; }
public MyClass()
{
Foos = new List<Foo>();
Bars = new List<Bar>();
}
}

public class Program
{
public static void Dump(object o)
{
Console.Write(o.GetType().FullName + ":");
foreach(PropertyInfo pi in
o.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
Console.Write(" ");
if(typeof(IList).IsAssignableFrom(pi.PropertyType))
{
Console.Write(pi.Name + "=[");
IList elms = (IList)pi.GetValue(o, null);
for(int i = 0; i < elms.Count; i++)
{
if(i != 0)
{
Console.Write(",");
}
Console.Write(elms[i]);
}
Console.Write("]");
}
else
{
Console.Write(pi.Name + "=" + pi.GetValue(o, null));
}
}
Console.WriteLine();
}
public static void Main(string[] args)
{
MyClass o = new MyClass();
o.Name = "Test";
o.Foos.Add(new Foo());
o.Bars.Add(new Bar());
o.Bars.Add(new Bar());
Dump(o);
Console.ReadKey();
}
}
}
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to fill a generics List with reflection? MrNobody Microsoft C# .NET 6 29th Apr 2010 10:54 AM
Getting list of Properties using Reflection Dawid Mostert Microsoft Dot NET 2 9th Jun 2005 03:52 PM
How to use reflection to list array values Manfred Braun Microsoft C# .NET 3 23rd Feb 2005 03:02 PM
Getting a list of constuctors and methods using reflection christopher green Microsoft C# .NET 3 19th Aug 2004 03:52 PM
list of assemblies for use with reflection memememe Microsoft C# .NET 1 10th Sep 2003 12:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:29 PM.