简体   繁体   English

从 C# 中的托管对象属性获取值的最佳方法

[英]Best Way to get values from Managed Objects Attribute in C#

When getting values Active Directory in C#, my normal process is to use the principal context to get most of these values.在 C# 中获取值 Active Directory 时,我的正常过程是使用主体上下文来获取大部分这些值。 There are still some things I need LDAP to get because they aren't available from a user principal object.我仍然需要 LDAP 来获取一些东西,因为它们不能从用户主体对象中获得。 For these attributes, I use the underlying object to get access to a directory entry.对于这些属性,我使用底层对象来访问目录条目。 I made a method to check for the existence of an property by checking if it is null.我通过检查属性是否为空来创建一个方法来检查属性是否存在。 If it isn't null, I return the value as a string.如果它不为空,我将值作为字符串返回。 I use code similar to what is listed below to accomplish this:我使用类似于下面列出的代码来完成此操作:

      if (directoryEntrygroup.Properties[directoryEntryPropertyName].Value != null)
            {
                returnValue = directoryEntrygroup.Properties[directoryEntryPropertyName].Value.ToString();
            }

That works pretty well for attributes that do not contain a list of values, such as a "cn" or "department."这对于不包含值列表的属性非常有效,例如“cn”或“部门”。 But for attributes such as "ManagedObjects" that do have some type of collection but returned as a object, what is the best way to get those values?但是对于诸如“ManagedObjects”之类的具有某种类型的集合但作为对象返回的属性,获取这些值的最佳方法是什么? For what its worth, this code is within a script task in SSIS for SQL Server 2012 using Framework 4.0.就其价值而言,此代码位于使用 Framework 4.0 的 SQL Server 2012 的 SSIS 中的脚本任务中。

I didn't realize when accessing a LDAP property, it was a collection, not a singular value.我在访问 LDAP 属性时没有意识到,它是一个集合,而不是一个单一的值。 If you just so happen to return a singular value, the code in my question works.如果您碰巧返回一个奇异值,那么我的问题中的代码有效。 But if you have more than one result returned, you will receive a System.Object[] instead.但是,如果您返回了多个结果,您将收到一个System.Object[] In my case, I have users (admins or managers) that have more than one object they manage which is why I received System.Object[]就我而言,我的用户(管理员或经理)管理的对象不止一个,这就是我收到System.Object[]

With that said, the object is return as a enumerable collection, even if only a single value is returned.话虽如此,对象作为可枚举集合返回,即使只返回一个值。

The way I solved the problem is by changing my code a little:我解决这个问题的方法是稍微改变我的代码:

      foreach (object thisvalue in directoryEntrygroup.Properties[directoryEntryPropertyName])
                    {
                       debug.writeline = thisvalue.ToString();                                                    
                    }

As far as I know, this solution will work for any LDAP property that returns more than one value, such as the "MemberOf" property for group objects据我所知,此解决方案适用于任何返回多个值的 LDAP 属性,例如组对象的“MemberOf”属性

Here is a article that talks about Property Value Collections:https://msdn.microsoft.com/en-us/library/ms180859(v=vs.80).aspx这是一篇关于属性值集合的文章:https ://msdn.microsoft.com/en-us/library/ms180859(v=vs.80).aspx

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM