将 WMI CimType 转换为 System.Type

发布于 2024-09-03 01:57:15 字数 1182 浏览 6 评论 0原文

我正在尝试编写一个通用扩展来将 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;
    }
}

我找到了一种我认为可行的方法 http://www.devcow.com/blogs/adnrg/archive/2005/09/23/108.aspx。然而,在我看来,可能有更好的方法,甚至是我忽略的 .net 函数。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

童话 2024-09-10 01:57:15

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

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

Hi You can also try the following code:

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);
}
}
只是我以为 2024-09-10 01:57:15

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

The ParseExtract above isn't working for me, but it does after I remove the "-000" from the end of the strings:

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();
}
我早已燃尽 2024-09-10 01:57:15

这是我最终使用的功能,它是我在链接中发布的功能的修改形式。奇怪的是,没有系统功能可以做到这一点。

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

Here is the function that I eventually used, it is a modified form of the one I posted in the link. It is odd, that there is no system function to do this.

    /**
    * <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;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文