mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-05-18 17:26:35 +00:00
Merge pull request #19 from koenbeuk/issue-18
Don't rewrite cast expressions
This commit is contained in:
@@ -136,11 +136,15 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
if (node.Parent is MemberAccessExpressionSyntax parentMemberAccessNode)
|
||||
{
|
||||
var targetSymbolInfo = _semanticModel.GetSymbolInfo(parentMemberAccessNode.Expression);
|
||||
if (targetSymbolInfo.Symbol is { Kind: SymbolKind.Parameter or SymbolKind.NamedType })
|
||||
if (targetSymbolInfo.Symbol is null)
|
||||
{
|
||||
rewrite = false;
|
||||
}
|
||||
if (targetSymbolInfo.Symbol?.ContainingType is not null)
|
||||
else if (targetSymbolInfo.Symbol is { Kind: SymbolKind.Parameter or SymbolKind.NamedType })
|
||||
{
|
||||
rewrite = false;
|
||||
}
|
||||
else if (targetSymbolInfo.Symbol?.ContainingType is not null)
|
||||
{
|
||||
if (!_compilation.HasImplicitConversion(targetSymbolInfo.Symbol.ContainingType, _targetTypeSymbol) ||
|
||||
!SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol))
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
using EntityFrameworkCore.Projectables;
|
||||
using Projectables.Repro;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.Generated
|
||||
#nullable disable
|
||||
{
|
||||
public static class Projectables_Repro_SomeExtensions_AsSomeResult
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Projectables.Repro.SomeEntity, string>> Expression()
|
||||
{
|
||||
return (global::Projectables.Repro.SomeEntity e) =>
|
||||
((global::Projectables.Repro.SuperEntity)e).Superpower;
|
||||
}
|
||||
}
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
using EntityFrameworkCore.Projectables;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.Generated
|
||||
#nullable disable
|
||||
{
|
||||
public static class _SomeExtensions_Test
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::SomeFlag, bool>> Expression()
|
||||
{
|
||||
return (global::SomeFlag f) =>
|
||||
f == global::SomeFlag.Foo;
|
||||
}
|
||||
}
|
||||
}
|
||||
+33
@@ -1045,6 +1045,39 @@ public static class SomeExtensions
|
||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Cast()
|
||||
{
|
||||
var compilation = CreateCompilation(@"
|
||||
using EntityFrameworkCore.Projectables;
|
||||
|
||||
namespace Projectables.Repro;
|
||||
|
||||
public class SuperEntity : SomeEntity
|
||||
{
|
||||
public string Superpower { get; set; }
|
||||
}
|
||||
|
||||
public class SomeEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public static class SomeExtensions
|
||||
{
|
||||
[Projectable]
|
||||
public static string AsSomeResult(this SomeEntity e) => ((SuperEntity)e).Superpower;
|
||||
}
|
||||
");
|
||||
|
||||
var result = RunGenerator(compilation);
|
||||
|
||||
Assert.Empty(result.Diagnostics);
|
||||
Assert.Single(result.GeneratedTrees);
|
||||
|
||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||
}
|
||||
|
||||
#region Helpers
|
||||
|
||||
Compilation CreateCompilation(string source, bool expectedToCompile = true)
|
||||
|
||||
Reference in New Issue
Block a user