Mr.Info
Mr.Info

Reputation: 77

How to loop through properties with specific index

I have a class with 50 properties and I like to loop the properties say from 8 to 24.

PropertyInfo[] properties = typeof(myclass).GetProperties(); 

foreach (PropertyInfo property in properties) // How to say loop from 8 to 24
{
     property.SetValue(property, value, null);
}

Upvotes: 0

Views: 48

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

To select range of items from a collection you can use Enumerable.Skip and Enumerable.Take.

 var range = typeof(myclass).GetProperties().Skip(8).Take(24-8);

Note that there is no formally defined order for properties (also it is unlikely that order returned by GetProperties will change at least on the same machine).

It is better to select groups of properties based on some well defined criteria like type, presence of particular custom attribute, visibility.

Upvotes: 1

Related Questions