gpt4 book ai didi

vb.net - VB.NET错误-使用结果时,在运行时可能会出现Null引用异常

转载 作者:行者123 更新时间:2023-12-02 10:49:39 24 4
gpt4 key购买 nike

背景:我编写了将输出所有当前登录的用户Active Directory组名称的代码。我想要通过IdentityReference.Translate的组名(例如:Acomp_user_BIG),而不是通过IdentityReference.Value返回的当前用户的SID(例如:S-1-5-32-544)。

这是我使用的代码:

Public ReadOnly Property Groups As IdentityReferenceCollection
Get
Dim irc As IdentityReferenceCollection
Dim ir As IdentityReference
irc = WindowsIdentity.GetCurrent().Groups

For Each ir In irc
Dim account As IdentityReference = ir.Translate(GetType(NTAccount))
Debug.WriteLine(account.Value)
Next

End Get
End Property

不幸的是,我在“End Get”上遇到了错误:
   Warning  1   Property 'Groups' doesn't return a value on all code paths. 
A null reference exception could occur at run time when the
result is used.

有任何解决此错误的建议吗?

最佳答案

Public ReadOnly Property Groups As IdentityReferenceCollection
Get
Dim irc As IdentityReferenceCollection
Dim ir As IdentityReference
irc = WindowsIdentity.GetCurrent().Groups

For Each ir In irc
Dim account As IdentityReference = ir.Translate(GetType(NTAccount))
Debug.WriteLine(account.Value)
Next

**return irc**
End Get
End Property

但是,通过查看代码可以修改irc,因此您应该创建一个新的IdentityReferenceCollection(假设它具有.add()方法),然后执行.add(account)
并返回您的新收藏

关于vb.net - VB.NET错误-使用结果时,在运行时可能会出现Null引用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5821199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com