예제 #1
0
        /// <summary>
        /// Returns calling flags.
        /// </summary>
        /// <param name="type">Type to check.</param>
        /// <param name="attribute">Type to check.</param>
        /// <returns>Binding flags.</returns>
        virtual protected BindingFlags GetFlags(Type type, XmlClassSerializable attribute)
        {
            bool deep = IsDeepSerializable(type);

            BindingFlags flags = attribute != null && attribute.Flags != BindingFlags.Default ? attribute.Flags : BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;

            if (!IsBaseType(type) && (attribute == null || attribute.Flags == BindingFlags.Default))
            {
                flags |= BindingFlags.Instance;
            }
            else if (IsBaseType(type))
            {
                flags = BindingFlags.Public | BindingFlags.Static;
            }

            if (attribute == null || attribute.Flags == BindingFlags.Default)
            {
                if (!deep)
                {
                    flags |= BindingFlags.DeclaredOnly;
                }
                else
                {
                    flags |= BindingFlags.FlattenHierarchy | BindingFlags.NonPublic;
                }
            }

            return(flags);
        }
예제 #2
0
        /// <summary>
        /// Fills a data.
        /// </summary>
        /// <param name="dataToFill">data to fill.</param>
        /// <param name="type">Type of the data.</param>
        /// <param name="parameters">Parameters used to fill.</param>
        /// <param name="serializableData">Reference SerializableData.</param>
        protected virtual void Filler(object dataToFill, Type type, object[] parameters, SerializableData serializableData)
        {
            XmlClassSerializable attribute = GetXmlClassSerializableAttribute(type);

            bool deep = IsDeepSerializable(type);
//            bool deep = attribute != null ? attribute.Deep : true;

//            BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.SetField | BindingFlags.SetProperty;
            BindingFlags flags = GetFlags(type, attribute);

            PropertyInfo property = GetProperty(type, serializableData.FieldName, flags);
            FieldInfo    field    = GetField(type, serializableData.FieldName, flags);

            try
            {
                if ((property != null && property.CanWrite) || (field != null && !field.IsLiteral))
                {
                    type.InvokeMember(serializableData.FieldName, flags, null, dataToFill, parameters);
                }
                else if (!type.BaseType.Equals(typeof(object)) && type.BaseType != null && deep)
                {
                    FillObjectField(dataToFill, type.BaseType, serializableData);
                }
                else if (IsCollection(type) && !IsArray(type))
                {
                    InvokeAddingMethod(dataToFill, parameters);
                }
            }
            catch
            {
                throw new XmlSerializationException(dataToFill, serializableData, property, field);
            }
        }
예제 #3
0
        /// <summary>
        /// Returns calling flags.
        /// </summary>
        /// <param name="type">Type to check.</param>
        /// <param name="attribute">Type to check.</param>
        /// <returns>Binding flags.</returns>
        protected override BindingFlags GetFlags(Type type, XmlClassSerializable attribute)
        {
            BindingFlags flags = base.GetFlags(type, attribute);

            flags |= BindingFlags.SetField | BindingFlags.SetProperty;

            return(flags);
        }
예제 #4
0
        /// <summary>
        /// Checks if a type with XmlClassSerializable attribute must be deep serialize.
        /// </summary>
        /// <param name="type">Type to check.</param>
        /// <returns>True if it must be deep serialize.</returns>
        virtual protected bool IsDeepSerializable(Type type)
        {
            XmlClassSerializable attribute = GetXmlClassSerializableAttribute(type);

            if (attribute == null)
            {
                return(false);
            }

            return(attribute.Deep);
        }
예제 #5
0
        /// <summary>
        /// Create a SerializableData from an object. FieldName will be empty.
        /// </summary>
        /// <param name="data">Reference Data.</param>
        /// <param name="serializableData">new SerializableData.</param>
        virtual protected void CreateSerializableData(object data, SerializableData serializableData)
        {
            Type type = data.GetType();

            bool foundAttribute            = true;
            XmlClassSerializable attribute = GetXmlClassSerializableAttribute(type);

            if (attribute == null || attribute.TagName == string.Empty)
            {
                foundAttribute = false;
            }

            serializableData.Type     = type.FullName;
            serializableData.Assembly = type.Assembly.ToString();
            serializableData.AssemblyQualifiedName = type.AssemblyQualifiedName;
            serializableData.Value   = foundAttribute ? string.Empty : data.ToString();
            serializableData.TagName = foundAttribute ? attribute.TagName : type.Name;
        }
예제 #6
0
        /// <summary>
        /// Finds the XmlClassSerializable attribute of the data and fill the SerializableData.
        /// </summary>
        /// <param name="data">Data to serialize.</param>
        /// <param name="serializableData">SerializableData to fill with found attributes.</param>
        virtual protected bool FindXmlSerializableClassAttribute(object data, SerializableData serializableData)
        {
            Type type = data.GetType();
            XmlClassSerializable attribute = GetXmlClassSerializableAttribute(type);

            if (attribute == null)
            {
                return(false);
            }

            CreateSerializableData(data, serializableData);

            Collection <DataMember> dataMembers = new Collection <DataMember>();

            FillDataMembers(type, dataMembers, true, attribute.Deep, GetFlags(type, attribute));
            FindClassFields(data, serializableData, dataMembers);

            return(true);
        }
예제 #7
0
        /// <summary>
        /// If the data derives from System.Collections.ICollection, it fills serializableData.
        /// Can override this function to customize the filling in a user collection.
        /// </summary>
        /// <param name="data">Data to check.</param>
        /// <param name="serializableData">SerializableData to fill.</param>
        virtual protected bool FillCollection(object data, SerializableData serializableData)
        {
            if (!IsCollection(data))
            {
                return(false);
            }

            System.Collections.ICollection collection = data as System.Collections.ICollection;

            foreach (object internalData in collection)
            {
                SerializableData newSerializableData = new SerializableData();

                XmlClassSerializable attribute = GetXmlClassSerializableAttribute(internalData.GetType());

                if (!FindXmlSerializableClassAttribute(internalData, newSerializableData))
                {
                    Type type = internalData.GetType();

                    Collection <DataMember> dataMembers = new Collection <DataMember>();

                    FillDataMembers(type, dataMembers, false, IsDeepSerializable(type), GetFlags(type, attribute));
                    if (dataMembers.Count == 0)
                    {
                        continue;
                    }

                    CreateSerializableData(internalData, newSerializableData);
                    FindClassFields(internalData, newSerializableData, dataMembers);
                }

                serializableData.SerializableDataCollection.Add(newSerializableData);
            }

            return(true);
        }
예제 #8
0
        /// <summary>
        /// Returns Binding flags.
        /// </summary>
        /// <param name="type">Type to check.</param>
        /// <returns>Binding flags.</returns>
        virtual protected BindingFlags GetFlags(Type type)
        {
            XmlClassSerializable attribute = GetXmlClassSerializableAttribute(type);

            return(GetFlags(type, attribute));
        }