Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-static members in C# generation #18655

Merged
merged 1 commit into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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