Friday, 2 March 2012

[discussion_vu] C# 4.0 Dynamic Object

Library Required
Microsoft.CSharp    (C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.CSharp.dll)

Imports
using System.Dynamic;
using System.Runtime.CompilerServices;

Conversion Functions
        public static List<ExpandoObject> DataTabeToClassObject(DataTable dt)
        {
            dynamic rows = new List<ExpandoObject>();
            foreach (DataRow r in dt.Rows)
            {
                rows.Add(DataRowToClassObject(r));
            }
            return rows;
        }
        public static ExpandoObject DataRowToClassObject(DataRow row)
        {
            dynamic obj = new ExpandoObject();
            foreach (DataColumn c in row.Table.Columns)
            {
                RuntimeOps.ExpandoTrySetValue(obj, null, -1, row[c], c.ColumnName, true);
            }
            return obj;
        }

Sample Code How to Use
DataTable table = ExecuteQuery("select * from tblMembers");

dynamic list = Conversion.DataTabeToClassObject(table);
dynamic d = list[0];
string name = d.MemberName;

ALLAH Hafiz
(ALLAH Makes you success in your life and after death [AMEEN])


Regards,

Muhammad Liaquat
Software Engineer
Cell Phone:
+92-21-6005331,  +92-333-9996889

Email:
    muhammad.liaquat@gmail.com

--
You received this message because you are subscribed to the Google Groups "Virtual University of Pakistan" group.
To post to this group, send email to discussion_vu@googlegroups.com.
To unsubscribe from this group, send email to discussion_vu+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/discussion_vu?hl=en.

No comments:

Post a Comment