C# WMI查询的空引用处理
我试图查询给定WMI类中的所有内容,但每次调优测试时都会返回一个空引用。。理想情况下,我希望将查询字符串设置为Win32_BIOS中的C# WMI查询的空引用处理,c#,unit-testing,C#,Unit Testing,我试图查询给定WMI类中的所有内容,但每次调优测试时都会返回一个空引用。。理想情况下,我希望将查询字符串设置为Win32_BIOS中的select*。尝试控制台时出错。WriteLine(属性) 公共类属性值 { 公共财产价值() { } 公共属性值(字符串wmiClassName) { WmiClassName=WmiClassName; } 公共字符串WmiClassName{get;set;} 公共void测试字符串(字符串属性名称) { ManagementObjectSearcher
select*。尝试控制台时出错。WriteLine(属性)
公共类属性值
{
公共财产价值()
{
}
公共属性值(字符串wmiClassName)
{
WmiClassName=WmiClassName;
}
公共字符串WmiClassName{get;set;}
公共void测试字符串(字符串属性名称)
{
ManagementObjectSearcher Mosearch=新的ManagementObjectSearcher
(String.Format(“从{1}中选择{0}”,propertyName,WmiClassName));
var collection=moSearcher.Get();
var enu=collection.GetEnumerator();
{
foreach(集合中的ManagementBaseObject属性)
{
类型t=prop.Properties[propertyName].GetType();
t、 GetProperty(propertyName).GetValue(prop,null);
Console.WriteLine(prop.Properties[propertyName].Value);
}
}
}
[测试类]
公共类测试字符串
{
[测试方法]
public void TestThis()
{
PropertyValue propval=新的PropertyValue(“Win32_BIOS”);
专有测试字符串(“制造商”);
}
}
}
问题在于此特定计算机在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");
}
}
}