c# - Take parameter, determine if its an object or list<>; if list<> then loop and treat each item as object -
aim
take object or list<>.
if it's object (done).
if it's list<> (any type of list<>) loop through each item, , similar thing object each item, returning dictionary or array (return type not important @ moment).
what have far, 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 need determine if it's object or list<>, i've tried sorts of generics, biggest problem i've come across casting list<> of unknown type.
any way of doing or understanding right steps, appreciated.
you this:
public static dictionary<string, object> pick(object obj, array picklist) { dictionary<string, object> dic = new dictionary<string, object> (); ienumerable items = obj ienumerable ?? new[] { obj }; foreach(string key in picklist) { foreach(object item in items) { dic.add(key, item.gettype().getproperty(key).getvalue(item, null)); } } return dic; }
note however, fail if try provide 2 or more items, since attempt add same key dictionary twice. fail if try pass object doesn't have 1 of specified properties. it's unclear want method in these cases, code seems example, you'll have figure detail out in real code.
Comments
Post a Comment