Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have base abstract class named Person, and I have some other calsses that inherited this class, I create a list of Person :
C#
List<person> baseP = new List<person>();
baseP[0].Add(new Worker("Yaakov",43));
baseP[1].Add(new Manager("David",54,"Intel"));
baseP[2].Add(new Teacher("Ella",33,"A"));


now I want to print this list and all properties of the workers types, each one of them has other additional propertis...

what is the best way to scan this list and print all properties ???

Thanks
Posted
Updated 14-Aug-15 2:33am
v2

You can use reflection to investigate the properties. For example you can list the properties of a type using GetProperties[^] method.

Few articles that might be interesting:
- Reflection in .NET[^]
- C#: Using Reflection and Custom Attributes to Map Object Properties[^]
- Looping through Object's properties in C#[^]
 
Share this answer
 
v2
This is easy.

Please see:
https://msdn.microsoft.com/en-us/library/58918ffs.aspx[^] (to get System.Type object from a class),
and then: https://msdn.microsoft.com/en-us/library/kyaxdd3x%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
I wouldn't go down the reflection road. It's slow, and it's ugly. Especially since you know all your object types beforehand.

Implement an abstract method in your base class and override it in your inherited classes to show the display value, or better yet, use ToString() overrides in all inherited classes. Both options give you full control over the information you want to display for each inherited type.

Next, just loop through the list and call ToString() or your own method on every object to display the necessary information.
 
Share this answer
 
Comments
Member 10304617 16-Aug-15 2:08am    
great idea...
and if there are some methods specific to class, the way to call them is
if(class is Worker)
(class as Worker).RequiredMethod();
??
kbrandwijk 16-Aug-15 7:35am    
That is possible, or again, create an abstract method for this in the base class (ExecuteSpecificMethods()) and override this method in the inherited classes. Then, you can just always call the ExecuteSpecificMethods method on every item in the list without type checking etc. Again, knowledge about the implementation should be put as close as possible to the implementation, in this case, in the inherited classes themselves, and preferably not where you process the list.
If this approach works for you, then please accept my solution as the answer to your question.
Member 10304617 16-Aug-15 8:40am    
thanks,
one Q - what is the best way to do it? How would you do it?
kbrandwijk 16-Aug-15 9:24am    
That really depends on the situation. I would use ToString() if what you're after is a simple human readable representation of your class (as the documentation states) for either GUI or dev/debug purposes. In your case, you want all properties of a class presented in a certain way, so I would use a separate method, and not use the ToString() override. But it all comes down to personal preference.
Member 10304617 16-Aug-15 9:26am    
it is not just to print properties, I also want to execute method that done something specific for the class

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900