mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-08-15 18:44:57 +00:00
Rewrite generated expressions to ignore nullability
This commit is contained in:
@@ -45,6 +45,7 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
|
||||
var expressionSyntaxRewriter = new ExpressionSyntaxRewriter(memberSymbol.ContainingType, semanticModel);
|
||||
var parameterSyntaxRewriter = new ParameterSyntaxRewriter(semanticModel);
|
||||
var returnTypeSyntaxRewriter = new ReturnTypeSyntaxRewriter(semanticModel);
|
||||
|
||||
var descriptor = new ProjectableDescriptor {
|
||||
ClassName = memberSymbol.ContainingType.Name,
|
||||
@@ -91,7 +92,7 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
return null;
|
||||
}
|
||||
|
||||
descriptor.ReturnTypeName = methodDeclarationSyntax.ReturnType.ToString();
|
||||
descriptor.ReturnTypeName = returnTypeSyntaxRewriter.Visit(methodDeclarationSyntax.ReturnType).ToString();
|
||||
descriptor.Body = expressionSyntaxRewriter.Visit(methodDeclarationSyntax.ExpressionBody.Expression);
|
||||
foreach (var additionalParameter in ((ParameterListSyntax)parameterSyntaxRewriter.Visit(methodDeclarationSyntax.ParameterList)).Parameters)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.Generator
|
||||
{
|
||||
public class ReturnTypeSyntaxRewriter : CSharpSyntaxRewriter
|
||||
{
|
||||
readonly SemanticModel _semanticModel;
|
||||
|
||||
public ReturnTypeSyntaxRewriter(SemanticModel semanticModel)
|
||||
{
|
||||
_semanticModel = semanticModel;
|
||||
}
|
||||
|
||||
public override SyntaxNode? VisitNullableType(NullableTypeSyntax node)
|
||||
{
|
||||
var typeInfo = _semanticModel.GetTypeInfo(node);
|
||||
if (typeInfo.Type is not null)
|
||||
{
|
||||
if (typeInfo.Type.TypeKind is not TypeKind.Struct)
|
||||
{
|
||||
return Visit(node.ElementType);
|
||||
}
|
||||
}
|
||||
|
||||
return base.VisitNullableType(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user