Exemple #1
0
        // private methods
        /// <summary>
        /// Creates a convention pack from the ConventionProfile.
        /// </summary>
        /// <returns></returns>
        private IEnumerable <IConvention> GetNewConventions()
        {
            var pack = new ConventionPack();

            // need to process defaults...
            pack.Append(DefaultConventionPack.Instance);

            // class mapping conventions
            if (MemberFinderConvention != null)
            {
                pack.Add(new MemberFinderConventionAdapter(MemberFinderConvention));
            }
            if (IdMemberConvention != null)
            {
                pack.Add(new IdMemberConventionAdapter(IdMemberConvention));
            }
            if (IdGeneratorConvention != null)
            {
                pack.Add(new IdGeneratorConventionAdapter(IdGeneratorConvention));
            }
            if (ExtraElementsMemberConvention != null)
            {
                pack.Add(new ExtraElementsConventionAdapter(ExtraElementsMemberConvention));
            }
            if (IgnoreExtraElementsConvention != null)
            {
                pack.Add(new IgnoreExtraElementsConventionAdapter(IgnoreExtraElementsConvention));
            }

            // member mapping conventions
            if (DefaultValueConvention != null)
            {
                pack.Add(new DefaultValueConventionAdapter(DefaultValueConvention));
            }
            if (SerializeDefaultValueConvention != null)
            {
                pack.Add(new SerializeDefaultValueConventionAdapter(SerializeDefaultValueConvention));
            }
            if (IgnoreIfDefaultConvention != null)
            {
                pack.Add(new IgnoreIfDefaultConventionAdapter(IgnoreIfDefaultConvention));
            }
            if (IgnoreIfNullConvention != null)
            {
                pack.Add(new IgnoreIfNullConventionAdapter(IgnoreIfNullConvention));
            }
            if (SerializationOptionsConvention != null)
            {
                pack.Add(new SerializationOptionsConventionAdapter(SerializationOptionsConvention));
            }
            if (ElementNameConvention != null)
            {
                pack.Add(new ElementNameConventionAdapter(ElementNameConvention));
            }

            // still need to process attributes
            pack.Append(AttributeConventionPack.Instance);
            return(pack.Conventions);
        }
Exemple #2
0
        // public static methods
        /// <summary>
        /// Looks up the effective set of conventions that apply to a type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>The conventions for that type.</returns>
        public static IConventionPack Lookup(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            lock (__lock)
            {
                var pack = new ConventionPack();

                // append any attribute packs (usually just one) at the end so attributes are processed last
                var attributePacks = new List <IConventionPack>();
#pragma warning disable 618 //obsoleted by ConventionProfile
                ConventionProfile conventionProfile = null;
#pragma warning restore 618
                foreach (var container in __conventionPacks)
                {
                    if (container.Filter(type))
                    {
#pragma warning disable 618 //obsoleted by ConventionProfile
                        if (container.Pack is ConventionProfile)
                        {
                            conventionProfile = container.Pack as ConventionProfile;
                        }
#pragma warning restore 618

                        if (container.Name == "__attributes__")
                        {
                            attributePacks.Add(container.Pack);
                        }
                        else
                        {
                            pack.Append(container.Pack);
                        }
                    }
                }
                if (conventionProfile != null)
                {
                    // already includes the default attribute convention pack
                    return(conventionProfile);
                }

                foreach (var attributePack in attributePacks)
                {
                    pack.Append(attributePack);
                }

                return(pack);
            }
        }