Skip to main content

Posts

Showing posts from April, 2012

Dynaminism in .Net 4.0

few dayz ago came across THIS question.
the solution to this can easily be figured out by the new dynamic features introduced in .Net 4.0.
i have made a project to demonstrate this.
the main code in the project goes like this

publicstaticdynamic[] GetDynamicObject(DataTable dt, Dictionary<string, string> mapping)
        {
List<dynamic> returnList = newList<dynamic>();
foreach (DataRow row in dt.Rows)
            {
dynamic obj = newExpandoObject();
var objectDic = (IDictionary<string, object>)obj;
foreach (DataColumn column in dt.Columns)
                {
                    objectDic.Add(mapping!=null?mapping.ContainsKey(column.ColumnName) ? mapping[column.ColumnName] : column.ColumnName:column.ColumnName,row[column]);
                }
                returnList.Add(obj);
            }
return returnList.ToArray();
        }The static method here named "GetDynamicObject" simply takes a data table(which can be populated by the data in the database) and a dictionary of…