Loop Through An Objects Properties In C#
By Jon Skeet: More compact and modern syntax:
By Jon Skeet: More compact and modern syntax:
Although properties are technically very similar to methods, they are quite different in terms of their usage scenarios. They should be seen as smart fields. They have the calling syntax of fields, and the flexibility of methods. ✓ DO create get-only properties if the caller should not be able to change the value of the property. Keep in mind that if the type of the property is a mutable reference type, the property value can be changed even if the property…
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:
1 2 3 4 5 6 7 |
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).