C# 如何确定C中是否存在动态属性#

C# 如何确定C中是否存在动态属性#,c#,active-directory,C#,Active Directory,我正在使用下面的代码遍历一些Active Directory PrincipalSearcher主要结果。在将结果属性分配给我的_user对象的foreach循环中,我可以中断调试并查看result.EmailAddress值。当我尝试将result.EmailAddress分配给_user.EmailAddress时,代码甚至无法编译。我猜EmailAddress是result的一个动态属性。是否有方法检查此属性,以便我可以将用户的广告电子邮件地址添加到我的\u用户对象 private st

我正在使用下面的代码遍历一些Active Directory PrincipalSearcher主要结果。在将结果属性分配给我的_user对象的foreach循环中,我可以中断调试并查看result.EmailAddress值。当我尝试将result.EmailAddress分配给_user.EmailAddress时,代码甚至无法编译。我猜EmailAddress是result的一个动态属性。是否有方法检查此属性,以便我可以将用户的广告电子邮件地址添加到我的\u用户对象

 private static void GetAllActiveDirectoryUsers()
    {
        PrincipalContext context = new PrincipalContext(
        ContextType.Domain, Environment.UserDomainName);

        UserPrincipal user = new UserPrincipal(context);


        // create a principal searcher for running a search operation
        PrincipalSearcher pS = new PrincipalSearcher(user);

        // run the query
        PrincipalSearchResult<Principal> results = pS.FindAll();

        foreach (Principal result in results)
        {

            Console.WriteLine(result.DisplayName);
            Console.ReadKey();

            User _user = new User();

            _user.Description = result.Description;
            _user.DisplayName = result.DisplayName;
            _user.DistinguishedName = result.DistinguishedName;
            _user.Guid = result.Guid ?? null;
            _user.Name = result.Name;
            _user.Sid = result.Sid?.ToString();

            Users.Add(_user);
        }

    }
private static void getAllActiveditoryUsers()
{
PrincipalContext上下文=新PrincipalContext(
ContextType.Domain、Environment.UserDomainName);
UserPrincipal用户=新的UserPrincipal(上下文);
//创建用于运行搜索操作的主体搜索器
PrincipalSearcher pS=新PrincipalSearcher(用户);
//运行查询
PrincipalSearchResult结果=pS.FindAll();
foreach(结果中的主要结果)
{
Console.WriteLine(result.DisplayName);
Console.ReadKey();
用户_User=新用户();
_user.Description=结果.Description;
_user.DisplayName=result.DisplayName;
_user.differentiedName=result.differentiedName;
_user.Guid=result.Guid??空;
_user.Name=result.Name;
_user.Sid=result.Sid?.ToString();
用户。添加(_user);
}
}

您需要从
主体
获取
目录条目
对象并查询其属性

类似这样的情况-假设电子邮件存储在“mail”属性中

 var directoryEntry = result.GetUnderlyingObject() as DirectoryEntry;
 if (directoryEntry != null && directoryEntry.Properties.Contains("mail"))
 {
    _user.EmailAddress = directoryEntry.Properties[property].Value.ToString();
 }
下面是一个使用上述方法的扩展方法。它只需要一个字符串作为您正在搜索的属性

public static string GetPropertyValue(this Principal principal, string property)
{
    var directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
    if (directoryEntry != null && directoryEntry.Properties.Contains(property))
    {
        return directoryEntry.Properties[property].Value.ToString();
    }

    return null;
}

它不是一个动态属性
EmailAddress
是继承自
Principal
UserPrincipal
类的属性。您的
结果
实际上是类型
UserPrincipal
,这就是为什么您在调试时会看到该属性,但您是以类型
Principal
访问它的,该类型没有名为
EmailAddress
的属性,因此您无法在代码中使用该属性

如果您想访问
EmailAddress
属性,则需要将
结果
强制转换为
UserPrincipal
。由于您确定所有结果都将是用户对象,因此可以在
foreach
中执行此操作:

foreach (UserPrincipal result in results)

也许这会有所帮助:您需要使用。然后可以遍历属性或子对象。