如何提取 PropertyCollection 中的属性值?

问题描述 投票:0回答:8
如何提取 PropertyCollection 中属性的值?

如果我深入查看下面一行中的“属性”,我可以看到该值,但如何读取它?

foreach (string propertyName in result.Properties.PropertyNames) { MessageBox.Show(ProperyNames[0].Value.ToString()); <--Wrong! }
    
c#
8个回答
3
投票
使用其他答案中的一些提示,我设法使用下面的代码获得了我需要的东西:

ResultPropertyValueCollection values = result.Properties[propertyName]; if (propertyName == "abctest") { MessageBox.Show(values[0].ToString()); }
    

2
投票
试试这个:

foreach (string propertyName in result.Properties.PropertyNames) { MessageBox.Show(result.Properties[propertyName].ToString()); }

或者这个:

foreach (object prop in result.Properties) { MessageBox.Show(prop.ToString()); }

此外:框架中有几个不同的 PropertyCollections 类。 这些示例基于 System.Data 类,但您也可能使用 System.DirectoryServices 类。 然而,这些类都不是真正的“反射”。 反射指的是不同的东西——即 System.Reflection 命名空间加上几个特殊的运算符。


0
投票
属性名称在函数中应该是大写吗?

再次阅读,我不得不承认我对所有这些属性到底想要什么感到有点困惑。这是类属性值还是您想要的实例?


0
投票
Vb.NET

For Each prop As String In result.Properties.PropertyNames MessageBox.Show(result.Properties(prop).Item(0), result.Item(i).Properties(prt).Item(0)) Next

我认为 C# 看起来像这样......

foreach (string property in result.Properties.PropertyNames) { MessageBox.Show(result.Properties[property].Item[0]); }

如上所述,框架中有一些不同的属性集合。


0
投票
我不确定您要什么,但我认为问题是您看到的是属性名称而不是它们的值?

如果是这样,原因是您正在通过 PropertyCollection.PropertyNames 集合而不是 PropertyCollection.Values 集合进行枚举。尝试这样的事情:

foreach (object value in result.Properties.Values) { MessageBox.Show(property.ToString()); }


我假设这个问题引用了 System.DirectoryServices.PropertyCollection 类,而不是 System.Data.PropertyCollection,因为引用了 PropertyNames,但现在我不太确定。如果问题与 System.Data 版本有关,请忽略此答案。


0
投票
如果将值集合放在“if”中,则只有在实际需要时才检索它,而不是每次循环时都检索它。 只是一个建议...:)


0
投票
PropertyNames 在其他地方不是大写的,下面的代码可以工作并且会显示属性的名称,但我想读取该值。 “PropertyName”只是一个字符串。

foreach (string propertyName in result.Properties.PropertyNames) { MessageBox.Show(PropertyName.ToString()); }
    

-1
投票
尝试:

foreach (string propertyName in result.Properties.PropertyNames) { MessageBox.Show(properyName.ToString()); }
    
© www.soinside.com 2019 - 2024. All rights reserved.