How can I store all the properties of a class in an array of objects?
Posted
by Richard77
on Stack Overflow
See other posts from Stack Overflow
or by Richard77
Published on 2010-03-26T09:09:01Z
Indexed on
2010/03/26
9:13 UTC
Read the original article
Hit count: 324
c#3.0
Hello,
Let's say I've a class myClass which has few properties, such as property1, property2, perperty3, etc. Now, I'd like to populate an array with each of those properties so that, I can access each of them through its index. Is there an automatic way of doing so?
Here's an example from SportsStore (Pro ASPN.NET MVC/Steve Sanderson/Apress) on how to gather all the active controllers in the the 'Assembly'.
var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
where typeof(IController).IsAssignableFrom(t)
select t;
foreach(Type t in controllerTypes)
//Do something
I wonder if there is some thing like the one above I can use to collect (only) properties of a class and store them in a array, no matter each one's type value (int, string, or custom type)
I hope I was able to express myself clearly. Otherwise I can amend the text.
Thanks for helping.
© Stack Overflow or respective owner