2011-11-16 6 views
62

私は現在、クラスオブジェクトRecordのすべての値を設定しています。C#クラスのプロパティを繰り返します

これは現時点でプロパティをプロパティにプロパティを設定するために使用しているコードです。

// Loop through each field in the result set 
for (int i = 0; i <= resultItems.Length; i++) 
{ 

    Record newRecord = new Record() 
    { 
      itemtype = resultItems[i - (fieldCount - 0)], 
      itemdesc = resultItems[i - (fieldCount - 1)], 
      prodcode = resultItems[i - (fieldCount - 2)], 
      proddesc = resultItems[i - (fieldCount - 3)], 
      curstat = resultItems[i - (fieldCount -4)], 
      totfree = resultItems[i - (fieldCount -5)], 
      totphys = resultItems[i - (fieldCount -6)], 
      pcolgroup = resultItems[i - (fieldCount -7)], 
      scolgroup = resultItems[i - (fieldCount -8)], 
      totpo =  resultItems[i - (fieldCount - 9)], 
      totso =  resultItems[i - (fieldCount - 10)], 
      quality = resultItems[i - (fieldCount - 11)], 
      statusdesc = resultItems[i - (fieldCount - 12)], 
      groupcode = resultItems[i - (fieldCount - 13)], 
      qualitydes = resultItems[i - (fieldCount - 14)], 
      pcoldesc = resultItems[i - (fieldCount - 15)], 
      scoldesc = resultItems[i - (fieldCount - 16)], 
      pgroupdesc = resultItems[i - (fieldCount - 17)], 
    }; 
} 

すべてのプロパティ名をハードコードしなくても、各プロパティを動的に反復処理できますか?そのような

何か:あなたはおそらくこれを実行するためにリフレクションを使用することができ

// Create new Record instance 
Record newRecord = new Record(); 

for (int e = 0; e < propertyCount.Length - 1; e++) 
{ 
    newRecord[fieldname] = resultItems[i - (fieldCount - e)]; 
} 
+0

Reflectionを試しましたか? http://stackoverflow.com/questions/997747/c-sharp-reflection-accessing-the-fields-of-a-struct – kol

+1

plsこのリンクを見てくださいhttp://stackoverflow.com/questions/721441/c -sharp-how-it-through-classes-fields-and-set-properties –

+0

resultItems配列のプロパティとインデックスの関係をどこでどのように維持したいのかを説明できますか? –

答えて

140

。私が理解する限り、クラスのプロパティを列挙して値を設定することができます。あなたはこれを試して、あなたがプロパティの順序を理解していることを確認する必要があります。このアプローチの詳細については、MSDN Documentationを参照してください。ヒントについては

は、あなたはおそらくのような何かができる: valueはあなたが(そう、あなたの resultItems配列から)で書きたいしている値である

Record record = new Record(); 

PropertyInfo[] properties = typeof(Record).GetProperties(); 
foreach (PropertyInfo property in properties) 
{ 
    property.SetValue(record, value); 
} 

+0

* property.Getvalue(someOtherClassObjectHavingExacltySameProperties)*を*値*? (私はこの**プロパティを試しましたか?.SetValue(objAppointment、property.GetValue(temp)); **しかし、それは私に** {"オブジェクトはターゲットタイプと一致しません"} **) –

+0

@MalikAsif私は私は完全に理解しているかどうかはわかりませんが、異なるタイプのプロパティをロードしようとしているようです。あなたが例を挙げてSOに質問を投稿してここにリンクさせると、あなたを見ていきます。 –

12
// the index of each item in fieldNames must correspond to 
// the correct index in resultItems 
var fieldnames = new []{"itemtype", "etc etc "}; 

for (int e = 0; e < fieldNames.Length - 1; e++) 
{ 
    newRecord 
     .GetType() 
     .GetProperty(fieldNames[e]) 
     .SetValue(newRecord, resultItems[e]); 
} 
+0

これはうまく見えますが、フィールド名をハードコーディングするのではなく、動的に行うことを望んでいました。これは可能ですか?ありがとう – Luke

+0

また、私のクラスインスタンスでは 'GetProperty'プロパティが利用できないようです。 :/ – Luke

+0

いいえ、resultItemインデックスとプロパティ名の間のどこかにマッピングが必要です。 – jgauffin

1

はい、プロパティ名から正しいプロパティにマップするインデクサーをレコードクラスに作成できます。これはプロパティ名からプロパティへのバインディングをすべて1つの場所に保持します。例:

public class Record 
{ 
    public string ItemType { get; set; } 

    public string this[string propertyName] 
    { 
     set 
     { 
      switch (propertyName) 
      { 
       case "itemType": 
        ItemType = value; 
        break; 
        // etc 
      } 
     } 
    } 
} 

また、他の人が触れたように、リフレクションを使用します。

関連する問題