Skip to content

Commit

Permalink
Filter out hidden fields in BackingFieldConvention
Browse files Browse the repository at this point in the history
Fixes #7536
  • Loading branch information
AndriySvyryd committed Jun 6, 2017
1 parent 6a4a951 commit 818b813
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 40 deletions.
14 changes: 1 addition & 13 deletions src/EFCore/Internal/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,7 @@ public static string DisplayName([NotNull] this Type type, bool fullName = true)
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public static FieldInfo GetFieldInfo([NotNull] this Type type, [NotNull] string fieldName)
{
var typesInHierarchy = type.GetTypesInHierarchy().ToList();
foreach (var typeInHierarchy in typesInHierarchy)
{
var fields = typeInHierarchy.GetRuntimeFields().ToDictionary(f => f.Name);
FieldInfo fieldInfo;
if (fields.TryGetValue(fieldName, out fieldInfo))
{
return fieldInfo;
}
}
return null;
}
=> type.GetRuntimeFields().FirstOrDefault(f => f.Name == fieldName && !f.IsStatic);

private static void AppendGenericArguments(Type[] args, int startIndex, int numberOfArgsToAppend, StringBuilder sb, bool fullName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
Expand Down Expand Up @@ -64,7 +65,14 @@ protected virtual void Apply([NotNull] PropertyBase propertyBase)
protected virtual FieldInfo TryMatchFieldName(
[NotNull] Type entityType, [NotNull] Type propertyType, [NotNull] string propertyName)
{
var fields = entityType.GetRuntimeFields().ToDictionary(f => f.Name);
var fields = new Dictionary<string, FieldInfo>();
foreach (var field in entityType.GetRuntimeFields().Where(f => !f.IsStatic))
{
if (!fields.ContainsKey(field.Name))
{
fields[field.Name] = field;
}
}

var camelized = char.ToLowerInvariant(propertyName[0]) + propertyName.Substring(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public virtual InternalRelationshipBuilder Apply(InternalRelationshipBuilder rel

var foreignKey = relationshipBuilder.Metadata;

var fkPropertyOnPrincipal
var fkPropertyOnPrincipal
= FindForeignKeyAttributeOnProperty(foreignKey.PrincipalEntityType, foreignKey.PrincipalToDependent?.Name);

var fkPropertyOnDependent
var fkPropertyOnDependent
= FindForeignKeyAttributeOnProperty(foreignKey.DeclaringEntityType, foreignKey.DependentToPrincipal?.Name);

if (!string.IsNullOrEmpty(fkPropertyOnDependent)
Expand All @@ -62,10 +62,10 @@ var fkPropertyOnDependent
fkPropertyOnPrincipal = null;
}

var fkPropertiesOnPrincipalToDependent
var fkPropertiesOnPrincipalToDependent
= FindCandidateDependentPropertiesThroughNavigation(relationshipBuilder, pointsToPrincipal: false);

var fkPropertiesOnDependentToPrincipal
var fkPropertiesOnDependentToPrincipal
= FindCandidateDependentPropertiesThroughNavigation(relationshipBuilder, pointsToPrincipal: true);

if (fkPropertiesOnDependentToPrincipal != null
Expand Down Expand Up @@ -230,7 +230,7 @@ private string FindForeignKeyAttributeOnProperty(EntityType entityType, string n
foreach (var memberInfo in clrType.GetRuntimeProperties().Cast<MemberInfo>()
.Concat(clrType.GetRuntimeFields()))
{
if (memberInfo is PropertyInfo propertyInfo
if (memberInfo is PropertyInfo propertyInfo
&& FindCandidateNavigationPropertyType(propertyInfo) != null)
{
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/Metadata/Internal/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public static bool AreCompatible([NotNull] IReadOnlyList<Property> properties, [
&& ((property.PropertyInfo != null
&& entityType.ClrType.GetRuntimeProperties().FirstOrDefault(p => p.Name == property.Name) != null)
|| (property.FieldInfo != null
&& entityType.ClrType.GetRuntimeFields().FirstOrDefault(p => p.Name == property.Name) != null))));
&& entityType.ClrType.GetFieldInfo(property.Name) != null))));
}

/// <summary>
Expand Down
24 changes: 4 additions & 20 deletions src/Shared/SharedTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,31 +175,15 @@ public static IEnumerable<PropertyInfo> GetPropertiesInHierarchy(this Type type,
public static IEnumerable<MemberInfo> GetMembersInHierarchy(this Type type, string name)
{
// Do the whole hierarchy for properties first since looking for fields is slower.
var currentType = type;
do
foreach (var propertyInfo in type.GetRuntimeProperties().Where(pi => pi.Name == name && !(pi.GetMethod ?? pi.SetMethod).IsStatic))
{
var typeInfo = currentType.GetTypeInfo();
var propertyInfo = typeInfo.GetDeclaredProperty(name);
if (propertyInfo != null
&& !(propertyInfo.GetMethod ?? propertyInfo.SetMethod).IsStatic)
{
yield return propertyInfo;
}
currentType = typeInfo.BaseType;
yield return propertyInfo;
}
while (currentType != null);

currentType = type;
do
foreach (var fieldInfo in type.GetRuntimeFields().Where(f => f.Name == name && !f.IsStatic))
{
var fieldInfo = currentType.GetRuntimeFields().FirstOrDefault(f => f.Name == name && !f.IsStatic);
if (fieldInfo != null)
{
yield return fieldInfo;
}
currentType = currentType.GetTypeInfo().BaseType;
yield return fieldInfo;
}
while (currentType != null);
}

private static readonly Dictionary<Type, object> _commonTypeDictionary = new Dictionary<Type, object>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ public int OnBase
get { return _onBase; }
set { _onBase = value; }
}

public new int Unrelated = 1;
}

private class OfTheMoon
Expand All @@ -375,6 +377,8 @@ public int TheGreatGigInTheSky
}

protected int _onBase;

public int Unrelated = 2;
}
}
}

0 comments on commit 818b813

Please sign in to comment.