From b56b02d1149fd1a6c8c3b4c41f78a99fe9b68468 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Wed, 30 Oct 2019 16:10:21 +0100 Subject: [PATCH] Support non-static members in C# generation Fixes #18665 --- .../Design/Internal/CSharpHelper.cs | 19 ++++++++---- .../Design/Internal/CSharpHelperTest.cs | 31 ++++++++++++++----- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/EFCore.Design/Design/Internal/CSharpHelper.cs b/src/EFCore.Design/Design/Internal/CSharpHelper.cs index 793e4643388..8d4372aacaa 100644 --- a/src/EFCore.Design/Design/Internal/CSharpHelper.cs +++ b/src/EFCore.Design/Design/Internal/CSharpHelper.cs @@ -893,14 +893,21 @@ private bool HandleExpression(Expression expression, StringBuilder builder, bool if (memberExpression.Expression == null) { builder - .Append(Reference(memberExpression.Member.DeclaringType, useFullName: true)) - .Append('.') - .Append(memberExpression.Member.Name); - - return true; + .Append(Reference(memberExpression.Member.DeclaringType, useFullName: true)); + } + else + { + if (!HandleExpression(memberExpression.Expression, builder)) + { + return false; + } } - return false; + builder + .Append('.') + .Append(memberExpression.Member.Name); + + return true; } } diff --git a/test/EFCore.Design.Tests/Design/Internal/CSharpHelperTest.cs b/test/EFCore.Design.Tests/Design/Internal/CSharpHelperTest.cs index b71a2c058cb..f9d2b11f115 100644 --- a/test/EFCore.Design.Tests/Design/Internal/CSharpHelperTest.cs +++ b/test/EFCore.Design.Tests/Design/Internal/CSharpHelperTest.cs @@ -533,22 +533,35 @@ public void Literal_with_two_parameter_instance_factory_and_internal_cast() public void Literal_with_static_field() { var typeMapping = CreateTypeMappingSource( - v => Expression.Field(null, typeof(SimpleTestType).GetField(nameof(SimpleTestType.SomeField)))); + v => Expression.Field(null, typeof(SimpleTestType).GetField(nameof(SimpleTestType.SomeStaticField)))); Assert.Equal( - "Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType.SomeField", - new CSharpHelper(typeMapping).UnknownLiteral(SimpleTestType.SomeField)); + "Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType.SomeStaticField", + new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType())); } [ConditionalFact] public void Literal_with_static_property() { var typeMapping = CreateTypeMappingSource( - v => Expression.Property(null, typeof(SimpleTestType).GetProperty(nameof(SimpleTestType.SomeProperty)))); + v => Expression.Property(null, typeof(SimpleTestType).GetProperty(nameof(SimpleTestType.SomeStaticProperty)))); + + Assert.Equal( + "Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType.SomeStaticProperty", + new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType())); + } + + [ConditionalFact] + public void Literal_with_instance_property() + { + var typeMapping = CreateTypeMappingSource( + v => Expression.Property( + Expression.New(typeof(SimpleTestType)), + typeof(SimpleTestType).GetProperty(nameof(SimpleTestType.SomeInstanceProperty)))); Assert.Equal( - "Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType.SomeProperty", - new CSharpHelper(typeMapping).UnknownLiteral(SimpleTestType.SomeProperty)); + "new Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType().SomeInstanceProperty", + new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType())); } [ConditionalFact] @@ -672,8 +685,10 @@ protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters p internal class SimpleTestType { - public static readonly SimpleTestType SomeField = new SimpleTestType(); - public static SimpleTestType SomeProperty { get; } = new SimpleTestType(); + public static readonly int SomeStaticField = 8; + public readonly int SomeField = 8; + public static int SomeStaticProperty { get; } = 8; + public int SomeInstanceProperty { get; } = 8; public SimpleTestType() {