Exemplo n.º 1
0
        /// <summary>
        /// Get serialized property and field values.
        /// </summary>
        /// <param name="type">Instance type.</param>
        /// <param name="sorted">Are values returned as sorted dictionary.</param>
        /// <param name="attributeUpdater">Updater that is called to get wanted value. Can be null.</param>
        /// <returns>Dictionary of values.</returns>
        internal static IDictionary <string, GXSerializedItem> GetValues(Type type, bool sorted, UpdateAttributes attributeUpdater)
        {
            GXSerializedItem s;

            if (type.IsPrimitive || type == typeof(string) || type == typeof(Guid))
            {
                return(null);
            }
            IDictionary <string, GXSerializedItem> list;

            if (sorted)
            {
                list = new SortedDictionary <string, GXSerializedItem>(StringComparer.InvariantCultureIgnoreCase);
            }
            else
            {
                list = new Dictionary <string, GXSerializedItem>(StringComparer.InvariantCultureIgnoreCase);
            }
            bool         all = type.GetCustomAttributes(typeof(DataContractAttribute), true).Length == 0;
            BindingFlags flags;

            //If DataContractAttribute is not set get only public property values.
            if (all)
            {
                flags = BindingFlags.Instance | BindingFlags.Public;
            }
            else
            {
                flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
            }
            //Save properties.
            foreach (PropertyInfo it in type.GetProperties(flags))
            {
                DataMemberAttribute[] attr = (DataMemberAttribute[])it.GetCustomAttributes(typeof(DataMemberAttribute), true);
                //If value is not marked as ignored.
                if (((all && it.CanWrite) || attr.Length != 0) &&
                    it.GetCustomAttributes(typeof(IgnoreDataMemberAttribute), true).Length == 0)
                {
                    if (!list.ContainsKey(it.Name) && it.Name != "Capacity")
                    {
                        s        = new GXSerializedItem();
                        s.Target = it;
                        s.Type   = it.PropertyType;
                        string name;
                        if (attr.Length == 0 || string.IsNullOrEmpty(attr[0].Name))
                        {
                            name = it.Name;
                        }
                        else
                        {
                            name = attr[0].Name;
                        }
                        if (attributeUpdater != null)
                        {
                            attributeUpdater(type, it.GetCustomAttributes(true), s);
                        }
                        if ((s.Attributes & Attributes.Ignored) == 0)
                        {
                            if (!it.PropertyType.IsArray)
                            {
#if !NETSTANDARD2_0 && !NETSTANDARD2_1
                                s.Get = GXInternal.CreateGetHandler(it.PropertyType, it);
                                s.Set = GXInternal.CreateSetHandler(it.PropertyType, it);
#endif
                            }
                            list.Add(name, s);
                        }
                    }
                }
            }
            if (!all)
            {
                //Save data members.
                foreach (FieldInfo it in type.GetFields(flags))
                {
                    DataMemberAttribute[] attr = (DataMemberAttribute[])it.GetCustomAttributes(typeof(DataMemberAttribute), true);
                    if (attr.Length != 0)
                    {
                        if (!list.ContainsKey(it.Name))
                        {
                            s        = new GXSerializedItem();
                            s.Target = it;
                            s.Type   = it.FieldType;
                            string name;
                            if (attr.Length == 0 || string.IsNullOrEmpty(attr[0].Name))
                            {
                                name = it.Name;
                            }
                            else
                            {
                                name = attr[0].Name;
                            }
                            if (attributeUpdater != null)
                            {
                                attributeUpdater(type, it.GetCustomAttributes(true), s);
                            }
                            if ((s.Attributes & Attributes.Ignored) == 0)
                            {
                                if (!it.FieldType.IsArray)
                                {
#if !NETSTANDARD2_0
                                    s.Get = GXInternal.CreateGetHandler(it.FieldType, it);
                                    s.Set = GXInternal.CreateSetHandler(it.FieldType, it);
#endif
                                }
                                list.Add(name, s);
                            }
                        }
                    }
                }
            }
            return(list);
        }