将 WMI CimType 转换为 System.Type
我正在尝试编写一个通用扩展来将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您好,您还可以尝试以下代码:
Hi You can also try the following code:
上面的 ParseExtract 对我不起作用,但在我从字符串末尾删除“-000”后它就起作用了:
The ParseExtract above isn't working for me, but it does after I remove the "-000" from the end of the strings:
这是我最终使用的功能,它是我在链接中发布的功能的修改形式。奇怪的是,没有系统功能可以做到这一点。
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.