Array Indexing Properties of A Class

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2011-01-08T05:59:31Z Indexed on 2011/01/08 7:53 UTC
Read the original article Hit count: 158

Filed under:
|

I have a class that has several properties:

class Person
{
     string Name;
     int Age;
     DateTime BirthDate;
}

Then I have a sort of wrapper class with a List<Person>. Within this wrapper class I want to be able to do something like Wrapper["Name"] that returns a new List<string> using .Select(x=>x.Name).

How do I create a wrapper class around an IEnumerable that supports mapping a string to the Property name? Something like the pseudo code below, but obviously it doesn't work. I'm 99.9% sure the solution will have to use Reflection and thats fine.

class Wrapper
{
   List<Person> PersonList;

   List<dynamic> this[string Column]
   {
       return PersonList.Select(x => x.[Column]).ToList();
   }
}

This may not seem like a good design, but its a fix to eventually enable the correct design from .NET 2.0 days. As I have it right now, the data is stored in Columns, so there is actually a List of Lists within my class right now.

Using the above example there would be three ILists (with a string Title) Name, Age, and Birthdate.

Everything is currently predicated on addressing the columns by their "string" name. I'm trying to convert the data structure to row based with an IEnumberable interface to allow Linq eventually while still maintaining the functionality of my current code.

Is converting the code to a Row based IEnumberable a good idea?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ