1

I have what seems to be a simple problem but one that I can't seem to find answers to. I have a class with properties. One of those properties returns a List. I have a method that cycles through all properties of any kind of class and produces a TreeNode for that class (a communication log application). When I come across the property identified as a List, I don't know how to cast the property.GetValue properly. the property.PropertyType is known but what ever I try, I get a compilation error or a runtime error.

Here is what I'm trying to do...

foreach (PropertyInfo prop in props)
{
    if(prop.PropertyType.Namespace == "System.Collections.Generic")
    {
        List<object> oList = prop.GetValue(data, null);
        MessageBox.Show(oList.Count.ToString())
    }
}

If I put a breakpoint on the GetValue line, the prop parameter knows that it's a list of "myclass" items with three elements. I just can't cast it to either a list of objects (which would be fine) or cast it to a list of actual "myclass" elements which would be even better. How do I cast the return value of PropertyInfo.GetValue (an object) to its List?

Ghyath Serhal
  • 7,466
  • 6
  • 44
  • 60
  • If you do var oList = prop.GetValue() does it return the correct type? – taylonr Mar 16 '11 at 11:39
  • Related to [Casting List - covariance/contravariance problem](http://stackoverflow.com/questions/4931789/casting-listt-covariance-contravariance-problem). – Binary Worrier Mar 16 '11 at 11:49

3 Answers3

3

First of all, it's not enough (or needed in this case at all) to check the namespace. You can check if prop.PropertyType is an instance of ICollection (you can use IsAssignableFrom). I'm suggesting ICollection because you only seem to care about the Count.

You can then cast it to an ICollection (non-generic) and run Enumerable.Cast, like:

IEnumerable<MyClass> res = ((ICollection)prop.GetValue(data,null)).Cast<MyClass>();
MessageBox.Show(res.Count().ToString());

The advantage over converting directly to List is that this will work with any collection. But if you don't need that, you can try what the other answer suggests.

sinelaw
  • 16,205
  • 3
  • 49
  • 80
  • You are the man (gender inspecific of course)!!! The IEnumerable is what I needed. I can get away with casting to a list of objects as the recursive function takes objects rather than specific class types. This works perfectly. Thanks much. As for the IsAssignableFrom.... how do I use that? – Hendrik Vis Mar 16 '11 at 12:30
  • Try if (ICollection.IsAssignableFrom(prop.PropertyType)) { ... } – sinelaw Mar 16 '11 at 12:48
1

did you try

prop.GetValue(data, null) as List<YourClass>;
Ghyath Serhal
  • 7,466
  • 6
  • 44
  • 60
  • The heart of the problem is that "YourClass" will vary with each different object that I'm trying to log. The property knows what kind of list it is but I can't put anything programatic in the <>. If I do, I get a compiler error. I have this problem with the other answers too. If I want to do this by checking the property's type that would be the way to go but I can't seem to find what to put in the <> that will work. – Hendrik Vis Mar 16 '11 at 12:17
0

I guess you want contravariance.

Are you trying to do so?

List<object> list = (List<Product>)object;

List<object> and List<Product> wouldn't be treated as the same type.

C# 4.0 introduced covariance and contravariance for interface and delegate generic parameters, but anyway, IList<T> doesn't have contravariance.

In my humild opinion, I believe you'll need to reconsider your object graph!

PD: Just comment out if you want guidance for that!

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206