2010-05-25 61 views
3

我正在嘗試編寫一個通用擴展來將ManagementObjectCollection轉換爲DataTable。這只是爲了使我正在編寫的啓動腳本/程序更容易。我遇到了CimType的問題。我已經包含了我在下面寫的代碼。將WMI CimType轉換爲System.Type

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; 
    } 
} 

我發現了一種方法,我認爲它的工作原理是http://www.devcow.com/blogs/adnrg/archive/2005/09/23/108.aspx。但是,在我看來,似乎可能有更好的方法,甚至是我忽略的.net功能。

我想我沒有說清楚。我遇到的問題是我需要從System.Management.CimType轉換爲System.Type。我幾乎認爲這將是一個普遍的問題,但我想我試圖以一般方式解決它。

+0

用觸發問題的示例查詢更新您的問題。 – 2010-05-25 15:10:20

+0

我不確定你的意思,它是一個通用函數。 – 249076 2010-06-09 17:27:17

回答

2

嗨您也可以嘗試下面的代碼:

public static class CimConvert 
{ 

private readonly static IDictionary<CimType, Type> Cim2TypeTable = 
    new Dictionary<CimType, Type> 
     { 
      {CimType.Boolean, typeof (bool)}, 
      {CimType.Char16, typeof (string)}, 
      {CimType.DateTime, typeof (DateTime)}, 
      {CimType.Object, typeof (object)}, 
      {CimType.Real32, typeof (decimal)}, 
      {CimType.Real64, typeof (decimal)}, 
      {CimType.Reference, typeof (object)}, 
      {CimType.SInt16, typeof (short)}, 
      {CimType.SInt32, typeof (int)}, 
      {CimType.SInt8, typeof (sbyte)}, 
      {CimType.String, typeof (string)}, 
      {CimType.UInt8, typeof (byte)}, 
      {CimType.UInt16, typeof (ushort)}, 
      {CimType.UInt32, typeof (uint)}, 
      {CimType.UInt64, typeof (ulong)} 
     }; 

public static Type Cim2SystemType(this PropertyData data) 
{ 
    Type type = Cim2TypeTable[data.Type]; 
    if (data.IsArray) 
     type = type.MakeArrayType(); 
    return type; 
} 

public static object Cim2SystemValue(this PropertyData data) 
{ 
    Type type = Cim2SystemType(data); 
    if (data.Type == CimType.DateTime) 
     return DateTime.ParseExact(data.Value.ToString(), "yyyyMMddHHmmss.ffffff-000", CultureInfo.InvariantCulture); 
    return Convert.ChangeType(data.Value, type); 
} 
} 
+1

對於任何人在未來嘗試轉換日期時間,請參閱此答案:http://stackoverflow.com/questions/2359007/convert-wmi-win32-operatingsystem-installdate-to-mm-dd-yyyy-format-c-wpf/2360653#2360653 – smack0007 2011-07-11 11:35:41

0

這是我最終使用的功能,它是我在鏈接中發佈的功能。奇怪的是,沒有系統功能來做到這一點。

/** 
    * <summary> 
    * This function converts a WMI CimType to a System.Type 
    * It was copied from: http://www.devcow.com/blogs/adnrg/archive/2005/09/23/108.aspx 
    * </summary> 
    */ 
    private static System.Type ConvertCimType(PropertyData property) 
    { 
     System.Type tReturnVal = null; 

     switch (property.Type) 
     { 
      case CimType.Boolean: 
       tReturnVal = typeof(System.Boolean); 
       break; 

      case CimType.Char16: 
       tReturnVal = typeof(System.String); 
       break; 

      case CimType.DateTime: 
       tReturnVal = typeof(System.DateTime); 
       break; 

      case CimType.Object: 
       tReturnVal = typeof(System.Object); 
       break; 

      case CimType.Real32: 
       tReturnVal = typeof(System.Decimal); 
       break; 

      case CimType.Real64: 
       tReturnVal = typeof(System.Decimal); 
       break; 

      case CimType.Reference: 
       tReturnVal = typeof(System.Object); 
       break; 

      case CimType.SInt16: 
       tReturnVal = typeof(System.Int16); 
       break; 

      case CimType.SInt32: 
       tReturnVal = typeof(System.Int32); 
       break; 

      case CimType.SInt8: 
       tReturnVal = typeof(System.SByte); 
       break; 

      case CimType.String: 
       tReturnVal = typeof(System.String); 
       break; 

      case CimType.UInt16: 
       tReturnVal = typeof(System.UInt16); 
       break; 

      case CimType.UInt32: 
       tReturnVal = typeof(System.UInt32); 
       break; 

      case CimType.UInt64: 
       tReturnVal = typeof(System.UInt64); 
       break; 

      case CimType.UInt8: 
       tReturnVal = typeof(System.Byte); 
       break; 
     } 

     // do a final check 
     tReturnVal = CheckType(property, tReturnVal); 

     return tReturnVal; 
    } 


    private static System.Type CheckType(PropertyData property, System.Type itemType) 
    { 
     if (property.IsArray) 
     { 
      return System.Type.GetType(itemType.ToString() + "[]"); 

     } 
     else 
     { 
      return itemType; 
     } 
    } 
1

的ParseExtract以上不是爲我工作,但它確實後,我從字符串的結尾去掉「-000」 :

public static DateTime GetDateTimeValue(this PropertyData pd) 
{ 
    if (pd.Type == CimType.DateTime) 
    { 
     string format = "yyyyMMddHHmmss.ffffff"; 
     string val = pd.Value.ToString().Substring(0,format.Length); 
     DateTime ret = DateTime.ParseExact(val, format, System.Globalization.CultureInfo.InvariantCulture); 
     return ret; 
    } 
    throw new ArgumentException(); 
}