Skip to content

Commit

Permalink
Support non-static members in C# generation
Browse files Browse the repository at this point in the history
Fixes #18665
  • Loading branch information
roji committed Oct 30, 2019
1 parent 0228200 commit 0929120
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
19 changes: 13 additions & 6 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
31 changes: 23 additions & 8 deletions test/EFCore.Design.Tests/Design/Internal/CSharpHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,22 +533,35 @@ public void Literal_with_two_parameter_instance_factory_and_internal_cast()
public void Literal_with_static_field()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
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<SimpleTestType>(
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<SimpleTestType>(
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]
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 0929120

Please sign in to comment.