-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathHelpers.cs
More file actions
52 lines (42 loc) · 1.75 KB
/
Helpers.cs
File metadata and controls
52 lines (42 loc) · 1.75 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
47
48
49
50
51
52
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace MapDataReader;
internal static class Helpers
{
internal static bool IsDecoratedWithAttribute(this TypeDeclarationSyntax cdecl, string attributeName) =>
cdecl.AttributeLists
.SelectMany(x => x.Attributes)
.Any(x => x.Name.ToString().Contains(attributeName));
internal static string FullName(this ITypeSymbol typeSymbol) => typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
internal static string StringConcat(this IEnumerable<string> source, string separator) => string.Join(separator, source);
// returns all properties with public setters
internal static IEnumerable<IPropertySymbol> GetAllSettableProperties(this ITypeSymbol typeSymbol)
{
var result = typeSymbol
.GetMembers()
.Where(s => s.Kind == SymbolKind.Property).Cast<IPropertySymbol>() //get all properties
.Where(p => p.SetMethod?.DeclaredAccessibility == Accessibility.Public) //has a public setter?
.ToList();
//now get the base class
var baseType = typeSymbol.BaseType;
if (baseType != null)
result.AddRange(baseType.GetAllSettableProperties()); //recursion
return result;
}
//checks if type is a nullable num
internal static bool IsNullableEnum(this ITypeSymbol symbol)
{
//tries to get underlying non-nullable type from nullable type
//and then check if it's Enum
if (symbol.NullableAnnotation == NullableAnnotation.Annotated
&& symbol is INamedTypeSymbol namedType
&& namedType.IsValueType
&& namedType.IsGenericType
&& namedType.ConstructedFrom?.ToDisplayString() == "System.Nullable<T>"
)
return namedType.TypeArguments[0].TypeKind == TypeKind.Enum;
return false;
}
}