Validating C# base class constructor parameter

After running Code Analysis in VS2010 beta (FxCop for previous versions) I’m getting the following warning:

In externally visible method
‘Identity.Identity(WindowsIdentity)’,
validate parameter ‘windowsIdentity’
before using it.

The constructor is:

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

for a class defined as:

public class Identity : WindowsIdentity

My question is, how do I validate the windowsIdentity parameter? Should I validate it in the constructor, and throw an exception, or is there a better way to call this?

You can validate it in a static method:

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

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

    return ident.Token;
}

(I didn’t bother to look for the type of WindowsIdentity.Token, but you get the idea)

Leave a Comment