2014-03-13 41 views
-2

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

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"); 
     } 
    } 
} 
+4

通常你会调试你的单元测试,并找出哪一行抛出异常。你无法调试你的单元测试吗? –

+0

代码抛出错误在哪里? – MoonKnight

回答

0

时发生错误的问题是,这个特殊的机器在返回WMI空值。