Exemple #1
0
        protected virtual TypeMemberInfo GetFieldMemberInfo(Type type, List <FieldInfo> fields, out bool ignore)
        {
            var topLevelField = fields.First();
            int ordering      = 999;

            ignore = false;
            TypeMemberInfo memberInfo = null;

            foreach (var field in fields)
            {
                if (field.IsBackingField())
                {
                    // get information from property
                    var autoProperty = field.GetEncapsulatingAutoProperty();
                    PropertiesToExclude.Add(autoProperty.Name);
                    var allProperties = type.Properties(Flags.InstanceAnyVisibility, autoProperty.Name);
                    memberInfo = GetPropertyMemberInfo(type, allProperties.ToList(), out ignore);
                    if (memberInfo != null)
                    {
                        memberInfo.Name = field.Name;
                        memberInfo.Type = TypeMemberInfo.MemberType.Field;
                    }
                    else if (!ignore && !WhiteListMode)
                    {
                        memberInfo = new TypeMemberInfo(field.Name,
                                                        TypeMemberInfo.MemberType.Field,
                                                        autoProperty.Name,
                                                        field.FieldType,
                                                        false)
                        {
                            Order = ordering
                        }
                    }
                    ;
                    break;
                }
                else
                {
                    // If any of our AttributesToIgnore are found then set the ignore flag
                    if (_Settings.AttributesToIgnore.Any(attribute => field.HasAttribute(attribute.GetType())))
                    {
                        ignore = true;
                    }
                    var elementAttrib       = field.Attribute <XmlElementAttribute>();
                    var attribAttribute     = field.Attribute <XmlAttributeAttribute>();
                    var dataMemberAttribute = field.Attribute <DataMemberAttribute>();

                    if (ignore && (elementAttrib == null) && (attribAttribute == null) && (dataMemberAttribute == null))
                    {
                        break; // skip the current field
                    }
                    if ((elementAttrib != null) || (attribAttribute != null) || (dataMemberAttribute != null))
                    {
                        ignore = false;
                        string entityName;
                        if (attribAttribute != null)
                        {
                            entityName = attribAttribute.GetPropertyValue("Name").ToString();
                        }
                        else if (elementAttrib != null)
                        {
                            entityName = elementAttrib.GetPropertyValue("Name").ToString();
                            ordering   = (int)elementAttrib.GetPropertyValue("Order");
                        }
                        else
                        {
                            entityName = dataMemberAttribute.GetPropertyValue("Name").ToString();
                            ordering   = (int)dataMemberAttribute.GetPropertyValue("Order");
                        }

                        entityName = string.IsNullOrEmpty(entityName) ? field.Name : entityName;
                        ordering   = ordering == 0 ? 999 : ordering;
                        memberInfo = new TypeMemberInfo(field.Name,
                                                        TypeMemberInfo.MemberType.Field,
                                                        entityName,
                                                        field.FieldType,
                                                        attribAttribute != null)
                        {
                            Order = ordering
                        };
                        break;
                    }
                }
            }

            if ((memberInfo == null) && !ignore && !WhiteListMode)
            {
                memberInfo = new TypeMemberInfo(topLevelField.Name,
                                                TypeMemberInfo.MemberType.Field,
                                                topLevelField.Name,
                                                topLevelField.FieldType,
                                                false)
                {
                    Order = ordering
                }
            }
            ;
            return(memberInfo);
        }
Exemple #2
0
        protected virtual TypeMemberInfo GetPropertyMemberInfo(Type type, List <PropertyInfo> properties, out bool ignore)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (properties == null)
            {
                throw new ArgumentNullException("properties");
            }

            ignore = false;
            if (properties.Count == 0)
            {
                return(null);
            }

            int            ordering   = 999;
            TypeMemberInfo memberInfo = null;

            foreach (var property in properties)
            {
                // If any of our AttributesToIgnore are found then set the ignore flag
                ignore = _Settings.AttributesToIgnore.Any(attribute => property.HasAttribute(attribute.GetType()));
                var elementAttrib       = property.Attribute <XmlElementAttribute>();
                var attribAttribute     = property.Attribute <XmlAttributeAttribute>();
                var dataMemberAttribute = property.Attribute <DataMemberAttribute>();

                // If we are set to explicitly ignore then we break out now
                if (ignore && (elementAttrib == null) && (attribAttribute == null) && (dataMemberAttribute == null))
                {
                    break; // skip the current field
                }
                // If we found an element, attribute ot datamember attribute then we build type member info from
                if ((elementAttrib != null) || (attribAttribute != null) || (dataMemberAttribute != null))
                {
                    ignore = false;
                    string entityName;
                    if (attribAttribute != null)
                    {
                        entityName = attribAttribute.GetPropertyValue("Name").ToString();
                    }
                    else if (elementAttrib != null)
                    {
                        entityName = elementAttrib.GetPropertyValue("Name").ToString();
                        ordering   = (int)elementAttrib.GetPropertyValue("Order");
                    }
                    else
                    {
                        entityName = dataMemberAttribute.Name ?? string.Empty;
                        ordering   = dataMemberAttribute.Order;
                    }

                    entityName = string.IsNullOrEmpty(entityName) ? property.Name : entityName;
                    ordering   = ordering == -1 ? 999 : ordering;
                    memberInfo = new TypeMemberInfo(property.Name,
                                                    TypeMemberInfo.MemberType.Property,
                                                    entityName,
                                                    property.PropertyType,
                                                    attribAttribute != null)
                    {
                        Order = ordering
                    };
                    break;
                }
            }

            return(memberInfo);
        }