C# Function: Take parameter, determine if its an object or list; if list
then loop and treat each item as object
Aim
Take an object or list<>.
If it's an object then do something with it (done).
If it's a list<> (any type of list<>) then loop through each item, and do
similar thing as object to each item, returning a dictionary or array
(return type not important at the moment).
What I have so far, the object bit...
public static Dictionary<string, object> Pick (object obj, Array
Picklist) {
Dictionary<string, object> dic = new Dictionary<string, object> ();
foreach (string key in Picklist) {
dic.Add(key, obj.GetType ().GetProperty (key).GetValue (obj, null));
}
return dic;
}
So next I need to determine if it's an object or a list<>, I've tried all
sorts of generics, the biggest problem I've come across is casting it into
a list<> of unknown type.
Any way of doing it or understanding the right steps, would be much
appreciated.
No comments:
Post a Comment