Skip to content

Commit 8d5e95f

Browse files
committed
Fix: Solve problem with null reference
1 parent 0344538 commit 8d5e95f

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/Files.Core.SourceGenerator/Generators/RealTimeLayoutGenerator.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,12 @@ private static string GenerateClass(string namespaceName, string className, Spec
9292
// Usings
9393
var usings = Array.Empty<UsingDirectiveSyntax>();
9494

95-
// Field declaration: private IRealTimeLayoutService RTLayoutService;
95+
// Field declaration: private IRealTimeLayoutService;
9696
var fieldDeclaration = SyntaxFactory.FieldDeclaration(
9797
SyntaxFactory.VariableDeclaration(
9898
SyntaxFactory.IdentifierName(ServiceInterfaceName))
99-
.AddVariables(SyntaxFactory.VariableDeclarator(ServiceVariableName)
100-
.WithInitializer(SyntaxFactory.EqualsValueClause(
101-
SyntaxFactory.ParseExpression($"Ioc.Default.GetRequiredService<{ServiceInterfaceName}>()")))))
102-
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword), SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword))
99+
.AddVariables(SyntaxFactory.VariableDeclarator(ServiceVariableName)))
100+
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword))
103101
.AddAttributeLists(SourceGeneratorHelper.GetAttributeForField(nameof(RealTimeLayoutGenerator)));
104102

105103
// Method: InitializeContentLayout
@@ -149,6 +147,29 @@ private static IEnumerable<StatementSyntax> CreateContentLayoutBody(Specificatio
149147
{
150148
var statements = new List<StatementSyntax>();
151149

150+
if (isInitialize)
151+
{
152+
153+
statements.Add(
154+
SyntaxFactory.ExpressionStatement(
155+
SyntaxFactory.AssignmentExpression(
156+
SyntaxKind.SimpleAssignmentExpression,
157+
SyntaxFactory.IdentifierName(ServiceVariableName),
158+
SyntaxFactory.InvocationExpression(
159+
SyntaxFactory.MemberAccessExpression(
160+
SyntaxKind.SimpleMemberAccessExpression,
161+
SyntaxFactory.MemberAccessExpression(
162+
SyntaxKind.SimpleMemberAccessExpression,
163+
SyntaxFactory.IdentifierName("Ioc"),
164+
SyntaxFactory.IdentifierName("Default")),
165+
SyntaxFactory.GenericName(
166+
SyntaxFactory.Identifier("GetRequiredService"))
167+
.WithTypeArgumentList(
168+
SyntaxFactory.TypeArgumentList(
169+
SyntaxFactory.SingletonSeparatedList<TypeSyntax>(
170+
SyntaxFactory.IdentifierName(ServiceInterfaceName)))))))));
171+
}
172+
152173
if (type == SpecificationType.Window)
153174
{
154175
statements.Add(

0 commit comments

Comments
 (0)