reflection - Casting a C# List -


I think that is a simple problem, but the one who can not find the answer I have. I have a class with properties that returns a list of those properties. I have a method that produces tree anode for all cycles of any kind of class and that class (a communication log application). . When I get the property identified as a list, then I do not know how to put the property. Gate value is well known to Property.PropertyType, but I'll try once, I get a compilation error or a runtime error is.

What I'm trying to do here ...

  Foreach (Property Info PROP in Sahara) {If (prop.PropertyType.Namespace == "System. Collections.Generic ") {List & lt; Object & gt; Olist = Prop. Gate value (data, empty); If I put a breakpoint on the GetValue line, then the context parameter knows that this is a list of "MyClass" with the three elements. I can not put it in the list of objects (which will be fine) or put it in the list of actual "MyClass" elements which will be better. How do I remove PropertyInfo.GetValue (an object) return value in my list?   

First of all, this is not enough (or in this case all is needed) to check the namespace for. You can see that there is an example of prop.PropertyType (you can use). I am suggesting ICollection because you only care about calculation.

You can run it in an Icon (non-normal) and run, like:

  IEnumerable & LT; MyClass & gt; Res = ((Icolation) prop.GetValue (data, empty)). Cast & lt; MyClass & gt; (); MessageBox.Show (. Res.Count () ToString ());   

The advantage of converting directly to the list is that it will work with any collection. But if you do not need it, then you can try whatever other answers you can try.

Comments