I have a database object (a row), that has lots of properties (columns) that map to form fields (asp:textbox, asp:dropdownlist etc). I would like to transform this
var myDict = myObj.ToDictionary(); //returns all public fields & properties
.
public static class MyExtensions
{
public static Dictionary ToDictionary(this object myObj)
{
return myObj.GetType()
.GetProperties()
.Select(pi => new { Name = pi.Name, Value = pi.GetValue(myObj, null) })
.Union(
myObj.GetType()
.GetFields()
.Select(fi => new { Name = fi.Name, Value = fi.GetValue(myObj) })
)
.ToDictionary(ks => ks.Name, vs => vs.Value);
}
}