https://github.com/71/anyconstraint.analyzer
Analyzer that suppresses the CS0702 error, thus allowing any constraint to be used (including 'Delegate' and 'Enum').
https://github.com/71/anyconstraint.analyzer
analyzer constraints csharp dotnet roslyn
Last synced: about 1 month ago
JSON representation
Analyzer that suppresses the CS0702 error, thus allowing any constraint to be used (including 'Delegate' and 'Enum').
- Host: GitHub
- URL: https://github.com/71/anyconstraint.analyzer
- Owner: 71
- License: mit
- Created: 2017-07-28T16:03:39.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-11-09T22:15:57.000Z (over 8 years ago)
- Last Synced: 2026-05-08T20:12:21.249Z (about 1 month ago)
- Topics: analyzer, constraints, csharp, dotnet, roslyn
- Language: C#
- Homepage: https://preview.nuget.org/packages/AnyConstraint.Analyzer
- Size: 16.6 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
AnyConstraint.Analyzer
======================
A simple C# analyzer that suppresses the **CS0702**: "Constraint cannot be special class 'Delegate' / 'Enum' / '...' " error.
## Installation
Simply install the NuGet package, and the analyzer will be automatically added to your project.
```powershell
Install-Package AnyConstraint.Analyzer
```
## Usage
There is no usage. Having the analyzer referenced is enough. The following code should compile just fine:
```csharp
public static void Invoke(TDelegate del) where TDelegate : Delegate { ... }
public static void GetValues(T @enum) where T : Enum { ... }
```
Nonetheless, an error will be shown in Visual Studio. To hide it from the 'Error List' panel, you can add the following code above your method:
```csharp
[SuppressMessage("Compiler", "CS0702")]
```
Or, at the global level:
```csharp
[assembly: SuppressMessage("Compiler", "CS0702")]
```
## How does it work?
Using [Ryder](https://github.com/6A/Ryder), the internal call to [`Binder.IsValidConstraintType`](http://source.roslyn.io/#Microsoft.CodeAnalysis.CSharp/Binder/Binder_Constraints.cs,14be8263fd49892c) is replaced by a simple `return true` statement, thus allowing any type to be used as a constraint.