【问题标题】:Null reference handling for WMI queriesWMI 查询的空引用处理
【发布时间】:2014-03-13 16:22:44
【问题描述】:

我正在尝试查询给定 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");
        }
    }
}

【问题讨论】:

  • 通常你会调试你的单元测试并找出抛出异常的行。您无法调试您的单元测试吗?
  • 抛出错误的代码在哪里?

标签: c# unit-testing


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多