-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTypeParameterSymbolExtensions.cs
More file actions
46 lines (41 loc) · 1.41 KB
/
Copy pathTypeParameterSymbolExtensions.cs
File metadata and controls
46 lines (41 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
namespace InterfaceGenerator
{
internal static class TypeParameterSymbolExtensions
{
public static IEnumerable<string> EnumGenericConstraints(this ITypeParameterSymbol symbol)
{
// the class/struct/unmanaged/notnull constraint has to be the first
// and cannot be combined with one another
if (symbol.HasNotNullConstraint)
{
yield return "notnull";
}
else if (symbol.HasUnmanagedTypeConstraint)
{
yield return "unmanaged";
}
else if (symbol.HasValueTypeConstraint)
{
yield return "struct";
}
else if (symbol.HasReferenceTypeConstraint)
{
yield return symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated
? "class?"
: "class";
}
// types go in the middle
foreach (var constraintType in symbol.ConstraintTypes)
{
yield return constraintType.ToDisplayString();
}
// the new() constraint has to be the last
if (symbol.HasConstructorConstraint)
{
yield return "new()";
}
}
}
}