System.DirectoryServices.AccountManagement.PrincipalCollection - 如何检查主体是用户还是组?
考虑以下代码:
GroupPrincipal gp = ... // gets a reference to a group
foreach (var principal in gp.Members)
{
// How can I determine if principle is a user or a group?
}
基本上我想知道的是(基于成员集合)哪些成员是用户,哪些是组。根据它们的类型,我需要触发额外的逻辑。
Consider the following code:
GroupPrincipal gp = ... // gets a reference to a group
foreach (var principal in gp.Members)
{
// How can I determine if principle is a user or a group?
}
Basically what I want to know is (based on the members collection) which members are users and which are groups. Depending on what type they are, I need to fire off additional logic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单:
基本上,您只需使用
as
关键字转换为您感兴趣的类型 - 如果值为null
则转换失败 - 否则成功。当然,另一种选择是获取类型并检查它:
Easy:
Basically, you just cast to a type you're interested in using the
as
keyword - if the value isnull
then the cast failed - otherwise it succeeded.Of course, another option would be to get the type and inspect it: