CA1062: Ověření argumentů veřejné metody
Název_typu |
ValidateArgumentsOfPublicMethods |
CheckId |
CA1062 |
Kategorie |
Microsoft.design |
Změnit rozdělení |
Bez rozdělení |
Příčina
Jeden z argumentů odkaz externě viditelné metoda dereferences bez ověření, zda je tento argument null (Nothing v jazyce Visual Basic).
Popis pravidla
Všechny referenční argumenty, které jsou předány externě viditelné metody by měly být kontrolovány proti null.V případě potřeby vyvoláním ArgumentNullException Pokud je argument null.
Pokud metodu lze volat z neznámé sestavení, protože je deklarována jako veřejné nebo chráněné, by měl ověřit všechny parametry metody.Pokud metoda je navržen pro volat pouze známé sestavení, by měla provádět vnitřní metody a použít InternalsVisibleToAttribute atribut sestavení, která obsahuje metodu.
Jak opravit porušení
Chcete-li opravit porušení tohoto pravidla ověřit každý odkaz argument proti null.
Při potlačení upozornění
Pokud jste si jisti, že parametr dereferenced byl ověřen jiným voláním metody ve funkci, můžete potlačit varování od tohoto pravidla.
Příklad
Následující příklad ukazuje způsob, který porušuje pravidla a metody, která splňuje pravidla.
Imports System
Namespace DesignLibrary
Public Class Test
' This method violates the rule.
Sub DoNotValidate(ByVal input As String)
If input.Length <> 0 Then
Console.WriteLine(input)
End If
End Sub
' This method satisfies the rule.
Sub Validate(ByVal input As String)
If input Is Nothing Then
Throw New ArgumentNullException("input")
End If
If input.Length <> 0 Then
Console.WriteLine(input)
End If
End Sub
End Class
End Namespace
public class Person
{
public string Name { get; private set; }
public int Age { get; private set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
// Copy constructor CA1062 fires because other is dereferenced
// without being checked for null
public Person(Person other)
: this(other.Name, other.Age)
{
}
}
using System;
namespace DesignLibrary
{
public class Test
{
// This method violates the rule.
public void DoNotValidate(string input)
{
if (input.Length != 0)
{
Console.WriteLine(input);
}
}
// This method satisfies the rule.
public void Validate(string input)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
if (input.Length != 0)
{
Console.WriteLine(input);
}
}
}
}
V Visual Studio 2005, toto pravidlo nezjistí parametry jsou předávány na jiný způsob, který provede ověření.
Public Function Method(ByVal value As String) As String
EnsureNotNull(value)
' Fires incorrectly
Return value.ToString()
End Function
Private Sub EnsureNotNull(ByVal value As String)
If value Is Nothing Then
Throw (New ArgumentNullException("value"))
End If
End Sub
public class Person
{
public string Name { get; private set; }
public int Age { get; private set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
// Copy constructor
public Person(Person other)
: this(PassThroughNonNull(other).Name,
PassThroughNonNull(other).Age)
{
}
// Null check method
private static Person PassThroughNonNull(Person person)
{
if (person == null)
throw new ArgumentNullException("person");
return person;
}
}
public string Method(string value)
{
EnsureNotNull(value);
// Fires incorrectly
return value.ToString();
}
private void EnsureNotNull(string value)
{
if (value == null)
throw new ArgumentNullException("value");
}
Konstruktory kopie, které naplnit pole nebo vlastnosti, které jsou referenční objekty můžete také porušují pravidla CA1062.Zkopírovaný objekt, který je předaný konstruktoru kopie pravděpodobně dochází k narušení null (Nothing v jazyce Visual Basic).Narušení vyřešíte pomocí statické metody (sdílené v jazyce Visual Basic) zkontrolujte, že není zkopírovaný objekt null.
V následujících Person příklad třídy other objektu, který je předán Person může být kopie konstruktoru null.
public class Person
{
public string Name { get; private set; }
public int Age { get; private set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
// Copy constructor CA1062 fires because other is dereferenced
// without being checked for null
public Person(Person other)
: this(other.Name, other.Age)
{
}
}
V následujících revidované Person například other objekt, který je předaný konstruktoru kopie je nejprve zkontrolovat null v PassThroughNonNull metoda.
public class Person
{
public string Name { get; private set; }
public int Age { get; private set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
// Copy constructor
public Person(Person other)
: this(PassThroughNonNull(other).Name,
PassThroughNonNull(other).Age)
{
}
// Null check method
private static Person PassThroughNonNull(Person person)
{
if (person == null)
throw new ArgumentNullException("person");
return person;
}
}