Convalidare gli argomenti di metodi pubblici
Aggiornamento: novembre 2007
TypeName |
ValidateArgumentsOfPublicMethods |
CheckId |
CA1062 |
Categoria |
Microsoft.Design |
Breaking Change |
Non sostanziale |
Causa
Un metodo visibile esternamente risolve i riferimenti a uno dei relativi argomenti di riferimento senza verificare se tale argomento è null (Nothing in Visual Basic).
Descrizione della regola
È necessario che tutti gli argomenti di riferimento passati a metodi visibili esternamente vengano sottoposti a verifica per accertarsi che non corrispondano a valori null. Se possibile, quando l'argomento è null, verrà generata un'eccezione System.ArgumentNullException.
Correzione delle violazioni
Per correggere una violazione di questa regola, convalidare ciascun argomento di riferimento per accertarsi che non sia null.
Esclusione di avvisi
Non escludere un avviso da questa regola.
Esempio
Nell'esempio riportato di seguito viene illustrato un metodo che viola la regola e un metodo che la soddisfa.
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
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);
}
}
}
}
In Visual Studio 2005 questa regola presenta diverse limitazioni. Ad esempio,
non consente di rilevare che i parametri vengono passati a un altro metodo che esegue la convalida.
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 string Method(string value)
{
EnsureNotNull(value);
// Fires incorrectly
return value.ToString();
}
private void EnsureNotNull(string value)
{
if (value == null)
throw new ArgumentNullException("value");
}
Inoltre, non comprende gli operatori di corto circuito.
Public Function Method(ByVal value1 As String, ByVal value2 As String) As String
If value1 Is Nothing OrElse value2 Is Nothing Then
Throw New ArgumentNullException()
End If
' Fires incorrectly
Return value1.ToString() + value2.ToString()
End Function
public string Method(string value1, string value2)
{
if (value1 == null || value2 == null)
throw new ArgumentNullException(value1 == null ? "value1" : "value2");
// Fires incorrectly
return value1.ToString() + value2.ToString();
}