Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# foreach (property in object)... Is there a simple way of doing this?

I have a class containing several properties (all are strings if it makes any difference).
I also have a list, which contains many different instances of the class.

While creating some unit tests for my classes I decided I wanted to loop through each object in the list and then loop through each property of that object...

I thought doing this would be as simple as...

foreach (Object obj in theList) {      foreach (Property theProperties in obj)      {          do some stufff!!;      } } 

But this didnt work! :( I get this error...

"foreach statement cannot operate on variables of type 'Application.Object' because 'Application.Object' does not contain a public definition for 'GetEnumerator'"

Does anyone know of a way of doing this without tons of ifs and loops or without getting into anything too complex?

like image 809
Jammerz858 Avatar asked Mar 27 '12 15:03

Jammerz858


People also ask

Bahasa C digunakan untuk apa?

Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.

C dalam Latin berapa?

C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).

Bahasa C dibuat pertama kali oleh siapa dan tahun berapa?

Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.


2 Answers

Give this a try:

foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties()) {    // do stuff here } 

Also please note that Type.GetProperties() has an overload which accepts a set of binding flags so you can filter out properties on a different criteria like accessibility level, see MSDN for more details: Type.GetProperties Method (BindingFlags) Last but not least don't forget to add the "system.Reflection" assembly reference.

For instance to resolve all public properties:

foreach (var propertyInfo in obj.GetType()                                 .GetProperties(                                         BindingFlags.Public                                          | BindingFlags.Instance)) {    // do stuff here } 

Please let me know whether this works as expected.

like image 96
sll Avatar answered Sep 16 '22 20:09

sll


You can loop through all non-indexed properties of an object like this:

var s = new MyObject(); foreach (var p in s.GetType().GetProperties().Where(p => !p.GetGetMethod().GetParameters().Any())) {     Console.WriteLine(p.GetValue(s, null)); } 

Since GetProperties() returns indexers as well as simple properties, you need an additional filter before calling GetValue to know that it is safe to pass null as the second parameter.

You may need to modify the filter further in order to weed out write-only and otherwise inaccessible properties.

like image 32
Sergey Kalinichenko Avatar answered Sep 19 '22 20:09

Sergey Kalinichenko