C# 如何在 PropertyCollection 中提取属性的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/243683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# How do I extract the value of a property in a PropertyCollection?
提问by
How do I extract the value of a property in a PropertyCollection?
如何在 PropertyCollection 中提取属性的值?
If I drill down on the 'Properties' in the line below is visual studion I can see the value but how do I read it?
如果我深入查看下面一行中的“属性”是visual studion,我可以看到该值,但如何读取它?
foreach (string propertyName in result.Properties.PropertyNames)
{
MessageBox.Show(ProperyNames[0].Value.ToString()); <--Wrong!
}
回答by dove
is that propertynames meant to be upper case within function?
那个属性名在函数中是大写的吗?
Reading again, i have to admit to be a little confused exactly what you're after with all these properties. Is this the class property value or an instance you're after?
再次阅读时,我不得不承认对您对所有这些属性的追求有些困惑。这是类属性值还是您想要的实例?
回答by steve
Try:
尝试:
foreach (string propertyName in result.Properties.PropertyNames)
{ MessageBox.Show(properyName.ToString()); <--Wrong!
}
回答by Joel Coehoorn
Try this:
尝试这个:
foreach (string propertyName in result.Properties.PropertyNames)
{
MessageBox.Show(result.Properties[propertyName].ToString());
}
Or this:
或这个:
foreach (object prop in result.Properties)
{
MessageBox.Show(prop.ToString());
}
Also: there are a couple different PropertyCollections classes in the framework. These examples are based on the System.Data class, but you might also be using the System.DirectoryServices class. However, neither of those classes are really "reflection". Reflection refers to something different- namely the System.Reflection namespace plus a couple special operators.
另外:框架中有几个不同的 PropertyCollections 类。这些示例基于 System.Data 类,但您也可能使用 System.DirectoryServices 类。然而,这两个类都不是真正的“反射”。反射指的是不同的东西——即 System.Reflection 命名空间加上一些特殊的运算符。
回答by Joel Coehoorn
The PropertyNames is not in uppercase elsewhere, the code below works and would show the name of the property but I want to read the value. 'PropertyName' is just a string.
其他地方的 PropertyNames 不是大写的,下面的代码有效并且会显示属性的名称,但我想读取该值。'PropertyName' 只是一个字符串。
foreach (string propertyName in result.Properties.PropertyNames)
{
MessageBox.Show(PropertyName.ToString());
}
回答by Stu Mackellar
I'm not certain what you're asking for, but I think the problem is that you're seeing the property names instead of their values?
我不确定您要什么,但我认为问题在于您看到的是属性名称而不是它们的值?
If so, the reason is that you're enumerating through the PropertyCollection.PropertyNames collection and not the PropertyCollection.Values collection. Try something like this instead:
如果是这样,原因是您正在枚举 PropertyCollection.PropertyNames 集合而不是 PropertyCollection.Values 集合。试试这样的:
foreach (object value in result.Properties.Values)
{
MessageBox.Show(property.ToString());
}
I was assuming that this question referred to the System.DirectoryServices.PropertyCollection class and not System.Data.PropertyCollection because of the reference to PropertyNames, but now I'm not so sure. If the question is about the System.Data version then disregard this answer.
我假设这个问题是指 System.DirectoryServices.PropertyCollection 类而不是 System.Data.PropertyCollection 因为引用了 PropertyNames,但现在我不太确定。如果问题与 System.Data 版本有关,则忽略此答案。
回答by thismat
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
I think C# looks like this...
我认为 C# 看起来像这样......
foreach (string property in result.Properties.PropertyNames)
{
MessageBox.Show(result.Properties[property].Item[0]);
}
As noted above, there are a few different property collections in the framework.
如上所述,框架中有几个不同的属性集合。
回答by thismat
Using a few hints from above I managed to get what I needed using the code below:
使用上面的一些提示,我设法使用下面的代码获得了我需要的东西:
ResultPropertyValueCollection values = result.Properties[propertyName];
if (propertyName == "abctest")
{
MessageBox.Show(values[0].ToString());
}
Thanks to all.
谢谢大家。
回答by GalacticCowboy
If you put the value collection inside your "if", you would only retrieve it when you actually need it rather than every time through the loop. Just a suggestion... :)
如果将值集合放在“if”中,则只会在实际需要时检索它,而不是每次都通过循环进行检索。只是一个建议... :)