开发者

Filling list data into a data model class C#

I'm after some advice on the most efficient approach to stepping through a list of fields and filling in their values to a class object.

I currently accessing fields via (code isn't exact but you get the idea =] ):

private string fieldName;

private int fillValue;

SessionData rawdata = new SessionData();

var count = 1;

foreach (objecttype obj in list)
{

    fillValue = obj.valueA + obj.ValueB;

               if (count < 1开发者_开发知识库0)
             {
                 fieldName = "band0" + count;
             }
             else
             {
                 fieldName = "band" + count;

             }

             rawdata.GetType().GetProperty(fieldName).SetValue(rawdata, fillValue , null);

             count++;
}

This is the basic idea of how I'm filling fields "band01" to "band99" (for example) with values 1-99.

Is there any other more effective method for doing this besides writing an individula if statement for each field?

Thanks for your time.


Instead of the if statement, use

fieldName = String.Format( "band{0:00}", count)


fieldName = "band" + count.ToString("00");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜