14

ReSharperNullReferenceExceptionは、

WindowsIdentity windowsIdentity = new WindowsIdentity(WindowsIdentity.GetCurrent().Token);

MSDN doc を調べましたが、これについての言及はありませんでした。また、実行可能ファイルを実行する場合はログオンする必要があるため、意味がありません。

これは単なる ReSharper 検索パターンですか?

4

4 に答える 4

21

ILSpy を使用するGetCurrentGetCurrentInternal、と を逆コンパイルしたバージョンを確認できますGetCurrent

GetCurrent:

public static WindowsIdentity GetCurrent()
{
    return WindowsIdentity.GetCurrentInternal(TokenAccessLevels.MaximumAllowed, false);
}

GetCurrentInternal:

internal static WindowsIdentity GetCurrentInternal(TokenAccessLevels desiredAccess, bool threadOnly)
{
    int errorCode = 0;
    bool flag;
    SafeTokenHandle currentToken = WindowsIdentity.GetCurrentToken(desiredAccess, threadOnly, out flag, out errorCode);
    if (currentToken != null && !currentToken.IsInvalid)
    {
        WindowsIdentity windowsIdentity = new WindowsIdentity();
        windowsIdentity.m_safeTokenHandle.Dispose();
        windowsIdentity.m_safeTokenHandle = currentToken;
        return windowsIdentity;
    }
    if (threadOnly && !flag)
    {
        return null;
    }
    throw new SecurityException(Win32Native.GetMessage(errorCode));
}

からthreadOnly呼び出すときは常に falseGetCurrentでありcurrentToken、他の return ステートメントに対して有効でなければならないため、 null を取得するリスクはないと思いますWindowsIdentity

于 2013-04-14T12:14:10.993 に答える
1

分解による、null返品可能。

見る:GetCurrentInternal(TokenAccessLevels desiredAccess, bool threadOnly)

免責事項:私は特定の状態を分析するのが面倒です:)

于 2013-04-14T11:28:26.550 に答える