WMI查询的空引用处理

时间:2014-03-13 16:22:45

标签: c# unit-testing

我试图查询给定WMI类中的所有内容,但是每次调整测试时我都会返回一个空引用。我理想地想查询字符串为select * from Win32_BIOS,但我试图先将此错误排序。尝试Console.WriteLine(property)

时发生错误
public class PropertyValue
{
    public PropertyValue()
    {
    }

    public PropertyValue(string wmiClassName)
    {
        WmiClassName = wmiClassName;
    }

    public string WmiClassName { get; set; }


    public void TestString<T>(string propertyName)
    {
        ManagementObjectSearcher moSearcher = new ManagementObjectSearcher
            (String.Format("SELECT {0} FROM {1}", propertyName, WmiClassName));

        var collection = moSearcher.Get();
        var enu = collection.GetEnumerator();

        {
            foreach (ManagementBaseObject prop in collection)
            {
                Type t = prop.Properties[propertyName].GetType();
                t.GetProperty(propertyName).GetValue(prop, null);
                Console.WriteLine(prop.Properties[propertyName].Value);
            }
        }
    }

    [TestClass]
    public class TestStrings
    {

        [TestMethod]
        public void TestThis()
        {
            PropertyValue propval = new PropertyValue("Win32_BIOS");
            propval.TestString<string>("Manufacturer");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题是这个特定的机器在WMI中返回一个空值。