如何循环访问 PropertyCollection
任何人都可以提供如何循环遍历 System.DirectoryServices.PropertyCollection 并输出属性名称和值的示例吗?
我正在使用 C#。
@JaredPar - PropertyCollection 没有名称/值属性。 它确实有一个 PropertyNames 和 Values,类型为 System.Collection.ICollection。 我不知道构成 PropertyCollection 对象的 basline 对象类型。
再次@JaredPar - 我最初用错误的类型错误地标记了问题。 那是我的错。
更新:根据 Zhaph - Ben Duguid 的输入,我能够开发以下代码。
using System.Collections;
using System.DirectoryServices;
public void DisplayValue(DirectoryEntry de)
{
if(de.Children != null)
{
foreach(DirectoryEntry child in de.Children)
{
PropertyCollection pc = child.Properties;
IDictionaryEnumerator ide = pc.GetEnumerator();
ide.Reset();
while(ide.MoveNext())
{
PropertyValueCollection pvc = ide.Entry.Value as PropertyValueCollection;
Console.WriteLine(string.Format("Name: {0}", ide.Entry.Key.ToString()));
Console.WriteLine(string.Format("Value: {0}", pvc.Value));
}
}
}
}
Can anyone provide an example of how to loop through a System.DirectoryServices.PropertyCollection and output the property name and value?
I am using C#.
@JaredPar - The PropertyCollection does not have a Name/Value property. It does have a PropertyNames and Values, type System.Collection.ICollection. I do not know the basline object type that makes up the PropertyCollection object.
@JaredPar again - I originally mislabeled the question with the wrong type. That was my bad.
Update: Based on Zhaph - Ben Duguid input, I was able to develop the following code.
using System.Collections;
using System.DirectoryServices;
public void DisplayValue(DirectoryEntry de)
{
if(de.Children != null)
{
foreach(DirectoryEntry child in de.Children)
{
PropertyCollection pc = child.Properties;
IDictionaryEnumerator ide = pc.GetEnumerator();
ide.Reset();
while(ide.MoveNext())
{
PropertyValueCollection pvc = ide.Entry.Value as PropertyValueCollection;
Console.WriteLine(string.Format("Name: {0}", ide.Entry.Key.ToString()));
Console.WriteLine(string.Format("Value: {0}", pvc.Value));
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我在另一个帖子上发布了我的答案,然后发现这个帖子提出了类似的问题。
我尝试了建议的方法,但在转换为 DictionaryEntry 时总是收到无效的转换异常。 对于 DictionaryEntry,像 FirstOrDefault 这样的东西就很时髦。 因此,我只需这样做:
有了这个,我就可以轻松地直接通过 Key 查询任何属性。 使用合并和安全导航运算符允许默认为空字符串或其他内容。
如果我想查看所有道具,它是一个类似的 foreach。
请注意,“adUser”对象是 UserPrincipal 对象。
I posted my answer on another thread, and then found this thread asking a similar question.
I tried the suggested methods, but I always get an invalid cast exception when casting to DictionaryEntry. And with a DictionaryEntry, things like FirstOrDefault are funky. So, I simply do this:
With that in place, I can then easily query for any property directly by Key. Using the coalesce and safe navigation operators allows for defaulting to an empty string or whatever..
And if I wanted to look over all props, it's a similar foreach.
Note that the "adUser" object is the UserPrincipal object.
编辑我误读了OP,认为它说的是PropertyValueCollection而不是PropertyCollection。 留下帖子,因为其他帖子正在引用它。
我不确定我明白你在问什么你只是想循环遍历集合中的每个值吗? 如果是这样,此代码将起作用
打印出名称/值
EDIT I misread the OP as having said PropertyValueCollection not PropertyCollection. Leaving post up because other posts are referenceing it.
I'm not sure I understand what you're asking Are you just wanting to loop through each value in the collection? If so this code will work
Print out the Name / Value
如果您只想要几个项目,您实际上不必做任何神奇的事情...
使用语句:System、System.DirectoryServices 和 System.AccountManagement
You really don't have to do anything magical if you want just a few items...
Using Statements: System, System.DirectoryServices, and System.AccountManagement
我不确定为什么很难找到答案,但通过下面的代码,我可以循环所有属性并提取我想要的属性,并为任何属性重用该代码。
如果需要,您可以以不同的方式处理目录条目部分
I'm not sure why this was so hard to find an answer to, but with the below code I can loop through all of the properties and pull the one I want and reuse the code for any property.
You can handle the directory entry portion differently if you want
我认为有一个更简单的方法
I think there's an easier way
运行时在监视窗口中查看PropertyValueCollection的值来识别元素的类型,它包含& 您可以对其进行扩展以进一步查看每个元素具有哪些属性。
添加到@JaredPar的代码
编辑:PropertyCollection由 PropertyValueCollection 组成
See the value of PropertyValueCollection at runtime in the watch window to identify types of element, it contains & you can expand on it to further see what property each of the element has.
Adding to @JaredPar's code
EDIT: PropertyCollection is made up of PropertyValueCollection
PropertyCollection 有一个 PropertyName 集合 - 它是一个字符串集合(请参阅 PropertyCollection.Contains 和 PropertyCollection.Item 两者都采用字符串)。
您通常可以调用 GetEnumerator 来允许您使用通常的枚举方法枚举集合 - 在这种情况下,您将获得一个包含字符串键的 IDictionary,然后是每个项目/值的对象。
The PropertyCollection has a PropertyName collection - which is a collection of strings (see PropertyCollection.Contains and PropertyCollection.Item both of which take a string).
You can usually call GetEnumerator to allow you to enumerate over the collection, using the usual enumeration methods - in this case you'd get an IDictionary containing the string key, and then an object for each item/values.