简体   繁体   English

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

[英]Validating C# base class constructor parameter

After running Code Analysis in VS2010 beta (FxCop for previous versions) I'm getting the following warning: 在VS2010测试版中运行代码分析(以前版本的FxCop)后,我收到以下警告:

In externally visible method 'Identity.Identity(WindowsIdentity)', validate parameter 'windowsIdentity' before using it. 在外部可见方法'Identity.Identity(WindowsIdentity)'中,在使用之前验证参数'windowsIdentity'。

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? 我的问题是,如何验证windowsIdentity参数? 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) (我没有费心去寻找WindowsIdentity.Token的类型,但是你明白了)

I believe FXCop reports this error here because it thinks that you could encounter a NullReferenceException by accessing windowsIdentity when calling the base class constructor. 我相信FXCop在这里报告此错误是因为它认为在调用基类构造函数时访问windowsIdentity会遇到NullReferenceException。

One way to add a validation check for null would be to add a static private function to your class that that can check the WindowsIdentity parameter for null and take appropriate action: 为null添加验证检查的一种方法是向您的类添加一个静态私有函数,该函数可以检查WindowsIdentity参数是否为null并采取适当的操作:

private static WindowsIdentity ValidateIdentity( WindowsIdentity identity )
{
    if( identity == null )
        throw new ArgumentNullException( "identity" );
    // possibly some other validation checks here...

    return identity;        
}

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

Another approach would be to use the ternary operator to verify the parameter, as in: 另一种方法是使用三元运算符来验证参数,如:

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

But, what you should really ask yourself is what would you do? 但是,你真正应该问自己的是你会做什么? If you're simply going to throw an exception, it may be ok to let the code stand as is, since it will already through a NullReferenceException if the argument is null. 如果您只是要抛出异常,那么让代码保持原样可能是正确的,因为如果参数为null,它将已经通过NullReferenceException

It's complaining because if you pass NULL as windowsIdentity, then when the constructor chains to the base class it will throw a null reference exception. 它抱怨,因为如果你将NULL作为windowsIdentity传递,那么当构造函数链接到基类时,它将抛出一个空引用异常。

The best way to deal with it depends on your design. 处理它的最佳方式取决于您的设计。 You could check it for null like this: 您可以像这样检查null:

:base(windowsIdentity == null ? null : windowsIdentity.Token)

Or you could make another constructor in base class constructor that takes a WindowsIdentity as a parameter, and have that constructor do that part of the validation. 或者您可以在基类构造函数中创建另一个构造函数,该构造函数将WindowsIdentity作为参数,并让该构造函数执行验证的那一部分。 Basically there are tons of ways to deal with it, just use what works best in your situation. 基本上有很多方法可以解决它,只需使用在你的情况下最有效的方法。

As of C# 6.0 you can use null-coalescing operator combined with the null-conditional operator like this: 从C#6.0开始,您可以使用null-coalescing运算符null-conditional运算 结合使用,如下所示:

public Identity(WindowsIdentity winIdentity)
    : base(winIdentity?.Token ?? throw new ArgumentNullException(nameof(winIdentity)))
{
    init();
}

FX cop is telling you that the parameter can't be null, so if you really need it you should validate it somehow. FX警告告诉你参数不能为空,所以如果你真的需要它,你应该以某种方式验证它。 Since you are using it in the constructor, you probably want a value different from null, so you should be validating it there for FX cop stop annoying you.. 因为你在构造函数中使用它,你可能想要一个不同于null的值,所以你应该在那里验证它,因为FX警察会让你烦恼。

If you need a constructor with null, you should be having another constructor with no parameters. 如果你需要一个带null的构造函数,你应该有另一个没有参数的构造函数。

If you are not using it or you are validating it in another point, you can skip the alert. 如果您没有使用它或者您在另一个点验证它,则可以跳过警报。

To avoid the problem with FXcop, you should be throwing ArgumentNullException. 为了避免FXcop的问题,你应该抛出ArgumentNullException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM