C# Iterate through Class properties

前端 未结 4 919
猫巷女王i
猫巷女王i 2020-11-29 16:43

I\'m currently setting all of the values of my class object Record.

This is the code that I\'m using to populate the record at the moment, property by p

4条回答
  •  死守一世寂寞
    2020-11-29 17:40

    You could possibly use Reflection to do this. As far as I understand it, you could enumerate the properties of your class and set the values. You would have to try this out and make sure you understand the order of the properties though. Refer to this MSDN Documentation for more information on this approach.

    For a hint, you could possibly do something like:

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

    Where value is the value you're wanting to write in (so from your resultItems array).

提交回复
热议问题