Skip to content

Commit a3f1120

Browse files
committed
add GetInitializeValue extension method
1 parent 50132f8 commit a3f1120

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/SourceGeneration.Reflection.SourceGenerator/Extensions/TypeSymbolExtensions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp.Syntax;
23
using System.Linq;
34
using System.Text;
5+
using System.Threading;
46

57
namespace SourceGeneration.Reflection.SourceGenerator;
68

@@ -97,4 +99,32 @@ public static bool IsCompliantGenericEnumerableInterface(this ITypeSymbol type)
9799
return type.AllInterfaces.Any(x => x.IsGenericType && x.Name == "IEnumerable");
98100
}
99101

102+
public static string GetInitializeValue(this IFieldSymbol field, CancellationToken cancellationToken)
103+
{
104+
if (field.DeclaringSyntaxReferences.Length > 0)
105+
{
106+
var syntax = field.DeclaringSyntaxReferences[0].GetSyntax(cancellationToken);
107+
if (syntax is VariableDeclaratorSyntax declaration &&
108+
declaration.Initializer is EqualsValueClauseSyntax equalsValueClause)
109+
{
110+
return equalsValueClause.Value.ToFullString();
111+
}
112+
}
113+
return null;
114+
}
115+
116+
public static string GetInitializeValue(this IPropertySymbol property, CancellationToken cancellationToken)
117+
{
118+
if (property.DeclaringSyntaxReferences.Length > 0)
119+
{
120+
var syntax = property.DeclaringSyntaxReferences[0].GetSyntax(cancellationToken);
121+
if (syntax is PropertyDeclarationSyntax declaration &&
122+
declaration.Initializer is EqualsValueClauseSyntax equalsValueClause)
123+
{
124+
return equalsValueClause.Value.ToFullString();
125+
}
126+
}
127+
return null;
128+
}
129+
100130
}

0 commit comments

Comments
 (0)