C# Object Array to DataTable Extension Method

Method Extension DataTable to Array Object C#

DataGrid.DataSource = objectArray.ToDataTable<ObjectType>();

public static DataTable ToDataTable<T>(this object value)
{
    XmlSerializer serializer = new XmlSerializer(value.GetType());
    System.IO.StringWriter sw = new System.IO.StringWriter();
    serializer.Serialize(sw, value);

    DataSet ds = new DataSet();
    System.IO.StringReader reader = new System.IO.StringReader(sw.ToString());
    ds.ReadXml(reader);
    return ds.Tables[0];
}


Comments are closed