验证C#基类构造函数参数

Ste*_*eve 9 c# validation parameters class base

在VS2010测试版中运行代码分析(以前版本的FxCop)后,我收到以下警告:

在外部可见方法'Identity.Identity(WindowsIdentity)'中,在使用之前验证参数'windowsIdentity'.

构造函数是:

public Identity(WindowsIdentity windowsIdentity)
         : base(windowsIdentity.Token)
{
         init();
}
Run Code Online (Sandbox Code Playgroud)

对于定义为的类:

public class Identity : WindowsIdentity
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何验证windowsIdentity参数?我应该在构造函数中验证它,并抛出异常,还是有更好的方法来调用它?

San*_*ken 12

您可以使用静态方法验证它:

public Identity(WindowsIdentity windowsIdentity)
         : base(GetToken(windowsIdentity))
{
         init();
}

static Token GetToken(WindowsIdentity ident)
{
    if(ident == null)
        throw new ArgumentNullException("ident");

    return ident.Token;
}
Run Code Online (Sandbox Code Playgroud)

(我没有费心去寻找WindowsIdentity.Token的类型,但是你明白了)