C# Reflection List<T>

N

nobody knows

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.
 
A

Arne Vajhøj

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);
}
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();
}
}
}
 
Joined
Sep 19, 2012
Messages
1
Reaction score
0
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);
}
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();
}
}
}



I hope the following would suit for your needs.

Type ResponseInfo = MyClass.GetType();
//
foreach (PropertyInfo Pinfo in ResponseInfo.GetProperties())
{
if (Pinfo.PropertyType.Namespace == "System.Collections.Generic")
{
MessageBox.Show("Generics " + Pinfo.PropertyType.Namespace);
}
}

Thanks

Senthil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top