Exemplo n.º 1
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        private FieldInfo FindBackingField(
            IPropertyBase property,
            IDictionary <Type, Dictionary <string, FieldInfo> > fieldCache)
        {
            var typesInHierarchy = property.DeclaringEntityType.ClrType.GetTypesInHierarchy().ToList();

            var fieldName = property["BackingField"] as string;

            if (fieldName != null)
            {
                foreach (var type in typesInHierarchy)
                {
                    var       fields = GetFields(type, fieldCache);
                    FieldInfo fieldInfo;
                    if (fields.TryGetValue(fieldName, out fieldInfo))
                    {
                        if (!fieldInfo.FieldType.GetTypeInfo().IsAssignableFrom(property.GetClrType().GetTypeInfo()))
                        {
                            throw new InvalidOperationException(
                                      CoreStrings.BadBackingFieldType(
                                          fieldName,
                                          fieldInfo.FieldType.ShortDisplayName(),
                                          property.DeclaringEntityType.DisplayName(),
                                          property.Name,
                                          property.GetClrType().ShortDisplayName()));
                        }

                        return(fieldInfo);
                    }
                }

                throw new InvalidOperationException(
                          CoreStrings.MissingBackingField(property.DeclaringEntityType.DisplayName(), property.Name, fieldName));
            }

            foreach (var type in typesInHierarchy)
            {
                var fields    = GetFields(type, fieldCache);
                var fieldInfo = _fieldMatcher.TryMatchFieldName(property, property.GetPropertyInfo(), fields);
                if (fieldInfo != null)
                {
                    return(fieldInfo);
                }
            }

            return(null);
        }
Exemplo n.º 2
0
 /// <summary>
 ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
 ///     directly from your code. This API may change or be removed in future releases.
 /// </summary>
 public virtual PropertyAccessors Create([NotNull] IPropertyBase propertyBase)
 => (PropertyAccessors)_genericCreate
 .MakeGenericMethod(propertyBase.GetClrType())
 .Invoke(null, new object[] { propertyBase });