Evenimente
17 mar., 21 - 21 mar., 10
Alăturați-vă seriei de întâlniri pentru a construi soluții AI scalabile bazate pe cazuri de utilizare din lumea reală cu colegi dezvoltatori și experți.
Înregistrați-vă acumAcest browser nu mai este acceptat.
Faceți upgrade la Microsoft Edge pentru a profita de cele mai noi funcții, actualizări de securitate și asistență tehnică.
Property | Value |
---|---|
Rule ID | CA2247 |
Title | Argument passed to TaskCompletionSource constructor should be TaskCreationOptions enum instead of TaskContinuationOptions enum |
Category | Usage |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | As warning |
Constructing a System.Threading.Tasks.TaskCompletionSource
with a System.Threading.Tasks.TaskContinuationOptions enum value rather than a System.Threading.Tasks.TaskCreationOptions enum value.
Using System.Object.ReferenceEquals method to test one or more value types for equality.
The TaskCompletionSource type has a constructor that accepts a System.Threading.Tasks.TaskCreationOptions enum value, and another constructor that accepts a Object. Accidentally passing a System.Threading.Tasks.TaskContinuationOptions enum value instead of a System.Threading.Tasks.TaskCreationOptions enum value will result in calling the Object-based constructor: it will compile and run, but it will not have the intended behavior.
To fix the violation, replace the System.Threading.Tasks.TaskContinuationOptions enum value with the corresponding System.Threading.Tasks.TaskCreationOptions enum value.
// Violation
var tcs = new TaskCompletionSource<int>(TaskContinuationOptions.RunContinuationsAsynchronously);
// Fixed
var tcs = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
A violation of this rule almost always highlights a bug in the calling code, such that the code will not behave as the developer intended, with the TaskCompletionSource effectively ignoring the specified option. The only time it is safe to suppress the warning is if the developer actually intended to pass a boxed System.Threading.Tasks.TaskContinuationOptions as the object state argument to the TaskCompletionSource
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA2247
// The code that's violating the rule is on this line.
#pragma warning restore CA2247
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA2247.severity = none
To disable this entire category of rules, set the severity for the category to none
in the configuration file.
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Usage.severity = none
For more information, see How to suppress code analysis warnings..
Feedback pentru .NET
.NET este un proiect open source. Selectați un link pentru a oferi feedback:
Evenimente
17 mar., 21 - 21 mar., 10
Alăturați-vă seriei de întâlniri pentru a construi soluții AI scalabile bazate pe cazuri de utilizare din lumea reală cu colegi dezvoltatori și experți.
Înregistrați-vă acum