1

I have a List of objects and each object is a another List of objects, so for example, I am passing List<object> cars to one method and I want to get all the properties of one of the objects, but when I try to do:

var props = cars.First().GetType().GetProperties(BindingFlags.DeclaredOnly |
                                                         BindingFlags.Public |
                                                         BindingFlags.Instance);

It gives me the properties of System.Object (Count, Capacity, and my list of objects)

When I look at cars which in this case is called list, it looks like this:

enter image description here


Your code is incorrect. You have a method inside another method, which is not allowed. Therefore the compiler states it expects a }, before the public void Ergebnis().

Your code if you write it out is

1. using System;
2. using System.Windows.Forms;
3. namespace RunTimeCompiler {
4. public class Test {
5.     public static void Main() {
6.        public void Ergebnis() {
7.            MessageBox.Show((1 + 2 + 3).ToString());
8.        }
9.     }
10.}
11.}
12.}

Note that on line 6 you need to close the method scope for Main before declaring your next method. A correct program would be

using System;
using System.Windows.Forms;
namespace RunTimeCompiler {
public class Test {
    public static void Main() {
        new Test().Ergebnis();
    }
    public void Ergebnis() {
        MessageBox.Show((1 + 2 + 3).ToString());
    }
}
}
4

2 に答える 2

3

あなたが言ったように:各オブジェクトはオブジェクトの別のリストです。報告するもの (カウント、容量など)、その内部リストのプロパティです。もっと深く知りたい場合は、それを as としてキャストしIList、内部リスト内を見てください。

于 2013-06-15T16:56:46.587 に答える
1
List<List<object>> listOfCarObjects = new List<object>();

listOfCarObjects.Add(new List<object>());

LookAtCars(listOfCarObjects);

public void LookAtCars(List<objects> cars)
{
   foreach(var item in cars)
   {
         List<object> innerList = item as List<object>
         foreach(innerItem in innerList)
         {
             //do something with innerItem
         }
   }
}

そのようなものは役に立ちますか?

于 2013-06-15T16:59:10.603 に答える