Share via


CA1856: ConstantExpected 특성의 잘못된 사용

속성
규칙 ID CA1856
타이틀 ConstantExpected 특성의 잘못된 사용
범주 성능
수정 사항이 주요 변경인지 여부 주요 변경 아님
.NET 8에서 기본적으로 사용 오류로

원인

ConstantExpectedAttribute 매개 변수에 특성이 올바르게 적용되지 않습니다.

규칙 설명

이 규칙은 다음과 같은 특성의 잘못된 사용에 플래그를 지정 ConstantExpectedAttribute 합니다.

  • 또는 Max 값이 Min 매개 변수 형식과 호환되지 않습니다.
  • 매개 변수 형식은 특성에 ConstantExpectedAttribute 대해 지원되지 않습니다.
  • Min 값과 Max 값이 반전됩니다.
  • 또는 Max 값이 Min 매개 변수 값 범위 내에 맞지 않습니다.

위반 문제를 해결하는 방법

받은 특정 오류 메시지로 표시된 대로 코드를 수정합니다.

예시

다음 코드 조각은 CA1856 위반을 보여 줍니다.

using System.Diagnostics.CodeAnalysis;

// Violation - value not compatible with parameter type.
static void M1([ConstantExpected(Min = "a")] char val) { }
// Violation - unsupported type for attribute.
static void M2([ConstantExpected] decimal val) { }
// Violation - Min and Max values are inverted.
static void M3([ConstantExpected(Max = 0, Min = 1)] int val) { }
// Violation - value does not fit within the parameter value bounds.
static void M4([ConstantExpected(Min = long.MinValue)] int val) { }

다음 코드 조각은 위반을 해결합니다.

using System.Diagnostics.CodeAnalysis;

static void M1([ConstantExpected(Min = 'a')] char val) { }
static void M2(decimal val) { }
static void M3([ConstantExpected(Min = 0, Max = 1)] int val) { }
static void M4([ConstantExpected(Min = int.MinValue)] int val) { }

경고를 표시하지 않는 경우

이 규칙을 위반하면 코드의 오류가 표시되며 항상 수정되어야 합니다.