I am trying to write a generic extension to turn a ManagementObjectCollection into a DataTable. This is just to make things easier for a startup script/program I am writing. I have ran into a problem with CimType. I have included the code I have written so far below.
public static DataTable GetData(this ManagementObjectCollection objectCollection)
{
DataTable table = new DataTable();
foreach (ManagementObject obj in objectCollection)
{
if (table.Columns.Count == 0)
{
foreach (PropertyData property in obj.Properties)
{
table.Columns.Add(property.Name, property.Type);
}
}
DataRow row = table.NewRow();
foreach (PropertyData property in obj.Properties)
{
row[property.Name] = property.Value;
}
table.Rows.Add(row);
}
return table;
}
}
I have found the a method which I think will work at http://www.devcow.com/blogs/adnrg/archive/2005/09/23/108.aspx. However it seems to me like there may be a better way, or even a .net function I am overlooking.
I guess I didn't make it clear. The problem I am having is that I need to convert from System.Management.CimType to System.Type. I almost thought this would be a common problem, but I suppose I'm trying to solve it in a general way.