| 屬性 | 值 |
|---|---|
| 規則識別碼 | CA2208 |
| 職稱 | 必須正確執行個體化引數例外狀況 |
| 類別 | 使用方式 |
| 修正程式是中斷或非中斷 | 不中斷 |
| 在 .NET 10 中預設啟用 | 建議 |
原因
當方法具有 參數,而且會擲回例外狀況類型,也就是 或衍生自 時, ArgumentException預期會呼叫接受參數的 paramName 建構函式。 可能的原因包括下列情況:
- 對例外狀況類型的預設 (無參數) 建構函式進行呼叫,該 ArgumentException 建構函式也具有接受參數的
paramName建構函式。 - 不正確的字串自變數會傳遞至例外狀況類型的參數化建構函式,或衍生自 ArgumentException。 例如,引
paramName數與其中一個方法參數的名稱不符。 - 參數名稱會傳遞給例外類型的建構函式引數,該例外類型是
message,或衍生自 ArgumentException。
檔案描述
而不是呼叫預設建構函式,請呼叫其中一個建構函式多載,以允許提供更有意義的例外狀況訊息。 例外狀況訊息應以開發人員為目標,並清楚說明錯誤狀況,以及如何更正或避免例外狀況。
一和兩個字串建構ArgumentException函式及其衍生型別的簽章與位置和messageparamName參數不一致。 請確定使用正確的字串自變數呼叫這些建構函式。 簽章如下所示:
ArgumentException(string message)ArgumentException(string message, string paramName)ArgumentNullException(string paramName)ArgumentNullException(string paramName, string message)ArgumentOutOfRangeException(string paramName)ArgumentOutOfRangeException(string paramName, string message)DuplicateWaitObjectException(string parameterName)DuplicateWaitObjectException(string parameterName, string message)
如何修正違規
若要修正此規則的違規,請呼叫接受訊息、參數名稱或兩者的建構函式,並確定自變數適用於所呼叫的類型 ArgumentException 。
提示
Visual Studio 中有一個程式代碼修正程式代碼,無法正確定位參數名稱。 若要使用它,請將游標放在警告數據列上,然後按 Ctrl+。(句號)。 從 所呈現的選項清單中選擇 [交換自變數順序 ]。
如果參數名稱而非訊息傳遞至 ArgumentException(String) 方法,修正程式會提供切換至雙自變數建構函式的選項。
隱藏警告的時機
只有在使用正確的字串自變數呼叫參數化建構函式時,才能安全地隱藏此規則的警告。
隱藏警告
如果您只想要隱藏單一違規,請將預處理器指示詞新增至原始程式檔以停用,然後重新啟用規則。
#pragma warning disable CA2208
// The code that's violating the rule is on this line.
#pragma warning restore CA2208
[*.{cs,vb}]
dotnet_diagnostic.CA2208.severity = none
如需詳細資訊,請參閱 如何隱藏程式代碼分析警告。
設定程式代碼以分析
使用下列選項來設定程式代碼基底要執行此規則的部分。
您可以只針對此規則、針對它套用的所有規則,或針對套用至此類別的所有規則,或針對它套用的所有規則,設定此選項。 如需詳細資訊,請參閱 程式代碼品質規則組態選項。
包含特定 API 介面
您可以藉由設定 [api_surface] 選項,根據程式代碼基底的存取範圍,設定執行此規則的哪些部分。 例如,若要指定規則只應該針對非公用 API 介面執行,請將下列機碼/值組新增至 專案中的 .editorconfig 檔案:
dotnet_code_quality.CAXXXX.api_surface = private, internal
注意
以適用規則的標識碼取代 XXXX 的 CAXXXX 部分。
根據預設,CA2208 規則會套用至所有 API 介面(公用、內部和私用)。
範例
下列程式代碼顯示不正確地具現化 實例的 ArgumentNullException建構函式。
public class Book
{
public Book(string title)
{
Title = title ??
throw new ArgumentNullException("All books must have a title.", nameof(title));
}
public string Title { get; }
}
Public Class Book
Private ReadOnly _Title As String
Public Sub New(ByVal title As String)
' Violates this rule (constructor arguments are switched)
If (title Is Nothing) Then
Throw New ArgumentNullException("title cannot be a null reference (Nothing in Visual Basic)", "title")
End If
_Title = title
End Sub
Public ReadOnly Property Title()
Get
Return _Title
End Get
End Property
End Class
下列程式代碼會藉由切換建構函式自變數來修正先前的違規。
public class Book
{
public Book(string title)
{
Title = title ??
throw new ArgumentNullException(nameof(title), "All books must have a title.");
}
public string Title { get; }
}
Public Class Book
Private ReadOnly _Title As String
Public Sub New(ByVal title As String)
If (title Is Nothing) Then
Throw New ArgumentNullException("title", "title cannot be a null reference (Nothing in Visual Basic)")
End If
_Title = title
End Sub
Public ReadOnly Property Title()
Get
Return _Title
End Get
End Property
End Class
下列程式碼顯示一個方法,該方法錯誤地擲回 ArgumentNullExceptionparamName,但這些並不符合方法的任何參數。 規則會觸發,因為 description 是局部變數,而不是方法參數。
public class Product
{
public string? Description { get; set; }
public string Name { get; set; } = string.Empty;
}
public class Example
{
// Violates CA2208: 'description' is not a parameter of this method.
public void ProcessProduct(Product product)
{
string? description = product.Description;
if (description is null)
{
throw new ArgumentNullException(nameof(description), $"Product named {product.Name} had no description!");
}
// Process description...
}
}
Public Class Product
Public Property Description As String
Public Property Name As String = String.Empty
End Class
Public Class Example
' Violates CA2208: 'description' is not a parameter of this method.
Public Sub ProcessProduct(ByVal product As Product)
Dim description As String = product.Description
If description Is Nothing Then
Throw New ArgumentNullException(NameOf(description), $"Product named {product.Name} had no description!")
End If
' Process description...
End Sub
End Class
下列程式碼透過使用 InvalidOperationException 來修正先前的違規,這在物件狀態無效時是適當的。
public class Product
{
public string? Description { get; set; }
public string Name { get; set; } = string.Empty;
}
public class Example
{
// Fixed: Use InvalidOperationException for invalid object state.
public void ProcessProduct(Product product)
{
string? description = product.Description;
if (description is null)
{
throw new InvalidOperationException($"Product named {product.Name} had no description!");
}
// Process description...
}
}
Public Class Product
Public Property Description As String
Public Property Name As String = String.Empty
End Class
Public Class Example
' Fixed: Use InvalidOperationException for invalid object state.
Public Sub ProcessProduct(ByVal product As Product)
Dim description As String = product.Description
If description Is Nothing Then
Throw New InvalidOperationException($"Product named {product.Name} had no description!")
End If
' Process description...
End Sub
End Class