im читать с помощью объектов ManagementObjectSearcher и хочу распечатать их в namevalueCollection (desriptor.properties).
Чтобы достичь этого, мне нужно идентифицировать все данные и написать его для коллекции - если это массив любого типа, я также нуждаюсь в каждом элементе. для кода позже.
Код: Выделить всё
ManagementObjectCollection searchResults = new ManagementObjectSearcher(
"select * from " + key).Get(); // for testing use => "Win32_ComputerSystem"
int counter = 0;
foreach (ManagementBaseObject? searchResult in searchResults) {
if (searchResult is null)
continue;
counter += 1;
foreach (PropertyData? property in searchResult.Properties) {
if (property.Name is null) {
continue;
}
object tmpobj = searchResult.GetPropertyValue(property.Name);
string propertyname = counter + " | " + property.Name;
if (tmpobj is null) {
continue;
}
else if (tmpobj is IEnumerable tmpcollection) {
foreach (object item in tmpcollection) {
if (tmpobj is null) {
continue;
}
else {
descriptor.Properties.Add(propertyname, item.ToString());
}
}
}
else {
descriptor.Properties.Add(propertyname, tmpobj.ToString());
}
}
}
https://learn.microsoft.com/en-us/dotne ... on/structs> для проще проверки.
Код: Выделить всё
short[] shorts =
{
11,
22,
33,
623,
2321,
0
};
short shorter = 0;
object objshorts = shorts as object;
if (shorts is IEnumerable) ; // => true
if (shorts is IEnumerable) ;// => false
if (shorts is IEnumerable) ; // => false
if (shorter is object) ; // => true
[*] Почему это не работает?
[*] Что мне нужно изменить?
Подробнее здесь: https://stackoverflow.com/questions/794 ... oesnt-work