0
public class MyClass
{
    public MyClass()
    {
        myDictionary = new Dictionary<string, string>();
        myArray = new List<int>();
    }

    private Dictionary<string, string> myDictionary;

    public Dictionary<string, string> MyDictionary
    {
        get { return myDictionary; }
    }

    private List<int> myArray;

    public List<int> MyArray
    {
        get { return myArray; }
    }
}

static void Main(string[] args)
{
    var model = new MyClass();
    Type t = model.GetType();
    System.Reflection.PropertyInfo[] properties = t.GetProperties();
    //Add items to MyArray and MyDictionary in this model According to the properties using reflection
}

このモデルで MyArray と MyDictionary に項目を追加したい リフレクションを使ったプロパティによると。ご協力ありがとうございました !

4

2 に答える 2

0

にアイテムを追加するには、メソッドGeneric.List<T>を使用しますAdd

例:

MyArray.Add(1);

Generic.Dictonary<T>メソッドも使用しAddますが、キーと値の 2 つの値を指定します。

MyDictionary.Add("MyKey", "MyValue");

したがって、あなたはあなたをループしてPropertyInfo[]、必要なものをあなたのList<T>またはに追加することができますDictionary<T>

foreach(var prop in properties )
{
   MyArray.Add(a number from somewhere);

   MyDictionary("some key", "some value");
}
于 2013-08-15T02:55:01.843 に答える
0
var dictProp = properties.Single(t => t.Name = "MyDictionary");
var myDict = (Dictionary<string,string>)dictProp.GetValue(model, null);
myDict.Add("MyKey", "MyValue");
于 2013-08-15T02:58:31.120 に答える