C# 将WMI CimType转换为System.Type

C# 将WMI CimType转换为System.Type,c#,wmi,type-conversion,C#,Wmi,Type Conversion,我正在尝试编写一个通用扩展来将ManagementObjectCollection转换为DataTable。这只是为了让我正在编写的启动脚本/程序更容易。我遇到了CimType的问题。我已经在下面列出了我编写的代码 public static DataTable GetData(this ManagementObjectCollection objectCollection) { DataTable table = new DataTable();

我正在尝试编写一个通用扩展来将ManagementObjectCollection转换为DataTable。这只是为了让我正在编写的启动脚本/程序更容易。我遇到了CimType的问题。我已经在下面列出了我编写的代码

    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;
    }
}
我找到了一种我认为会奏效的方法。然而,在我看来,可能有更好的方法,甚至是我忽略的.net功能


我想我没有说清楚。我遇到的问题是,我需要从System.Management.CimType转换为System.Type。我几乎认为这是一个常见的问题,但我想我正在尝试以一种通用的方式解决它。

这是我最终使用的函数,它是我在链接中发布的函数的一种修改形式。奇怪的是,没有系统功能来实现这一点

    /**
    * <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;
        }
    }
/**
* 
*此函数用于将WMI CimType转换为System.Type
*复制自:http://www.devcow.com/blogs/adnrg/archive/2005/09/23/108.aspx
* 
*/
私有静态系统.Type ConvertCimType(PropertyData属性)
{
System.Type tReturnVal=null;
开关(property.Type)
{
case CimType.Boolean:
tReturnVal=typeof(System.Boolean);
打破
案例CimType.Char16:
tReturnVal=typeof(System.String);
打破
案例CimType.DateTime:
tReturnVal=类型(System.DateTime);
打破
案例类型。对象:
tReturnVal=类型(System.Object);
打破
案例CimType.Real32:
tReturnVal=类型(十进制系统);
打破
案例CimType.Real64:
tReturnVal=类型(十进制系统);
打破
案例类型。参考:
tReturnVal=类型(System.Object);
打破
案例CimType.SInt16:
tReturnVal=类型(System.Int16);
打破
案例CimType.SInt32:
tReturnVal=类型(System.Int32);
打破
案例CimType.SInt8:
tReturnVal=类型(System.SByte);
打破
大小写类型。字符串:
tReturnVal=typeof(System.String);
打破
案例CimType.UInt16:
tReturnVal=类型(System.UInt16);
打破
案例CimType.UInt32:
tReturnVal=类型(System.UInt32);
打破
案例CimType.UInt64:
tReturnVal=类型(系统UInt64);
打破
案例CimType.UInt8:
tReturnVal=类型(System.Byte);
打破
}
//做最后检查
tReturnVal=CheckType(属性,tReturnVal);
返回tReturnVal;
}
私有静态System.Type CheckType(PropertyData属性,System.Type itemType)
{
if(property.IsArray)
{
返回System.Type.GetType(itemType.ToString()+“[]”);
}
其他的
{
返回项目类型;
}
}

您好,您也可以尝试以下代码:

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);
}
}
公共静态类CimConvert
{
专用只读静态IDictionary Cim2TypeTable=
新词典
{
{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)}
};
公共静态类型Cim2SystemType(此属性数据)
{
Type Type=Cim2TypeTable[data.Type];
if(data.IsArray)
type=type.MakeArrayType();
返回类型;
}
公共静态对象Cim2SystemValue(此属性数据)
{
类型类型=Cim2SystemType(数据);
if(data.Type==CimType.DateTime)
return DateTime.ParseExact(data.Value.ToString(),“yyyyMMddHHmmss.ffffff-000”,CultureInfo.InvariantCulture);
返回Convert.ChangeType(data.Value,type);
}
}

上面的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();
}

使用触发问题的示例查询更新您的问题。我不确定您的意思,这是一个通用函数。对于将来试图转换datetime的任何人,请参阅以下答案: