如果我深入查看下面一行中的“属性”,我可以看到该值,但如何读取它?
foreach (string propertyName in result.Properties.PropertyNames)
{
MessageBox.Show(ProperyNames[0].Value.ToString()); <--Wrong!
}
ResultPropertyValueCollection values = result.Properties[propertyName];
if (propertyName == "abctest") {
MessageBox.Show(values[0].ToString());
}
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 命名空间加上几个特殊的运算符。
再次阅读,我不得不承认我对所有这些属性到底想要什么感到有点困惑。这是类属性值还是您想要的实例?
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]);
}
如上所述,框架中有一些不同的属性集合。
如果是这样,原因是您正在通过 PropertyCollection.PropertyNames 集合而不是 PropertyCollection.Values 集合进行枚举。尝试这样的事情:
foreach (object value in result.Properties.Values)
{
MessageBox.Show(property.ToString());
}
我假设这个问题引用了 System.DirectoryServices.PropertyCollection 类,而不是 System.Data.PropertyCollection,因为引用了 PropertyNames,但现在我不太确定。如果问题与 System.Data 版本有关,请忽略此答案。
foreach (string propertyName in result.Properties.PropertyNames)
{
MessageBox.Show(PropertyName.ToString());
}
foreach (string propertyName in result.Properties.PropertyNames) {
MessageBox.Show(properyName.ToString());
}