Beispiel #1
0
        /// <summary>
        /// This method will enumerate over all of the object's fields and generate the m_fields list.
        /// </summary>
        private void GenerateFieldData()
        {
            if (m_doNotSerialize) // if this class is not to be serialized, then don't find any fields for it.
            {
                return;
            }

            if (Type.Namespace == "System.Data.Entity.DynamicProxies")
            {
                return;
            }

            var allFields = Type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Public);

            // Go through each field found and process according to certain rules
            foreach (var fi in allFields)
            {
                var t = fi.FieldType;
                if (!CFramework.IsValidType(t))
                {
                    continue;
                }

                var fld = new CFieldData(fi, this);

                if (fld.DoNotSerialize) // If the field does not want to be serialized, then don't.
                {
                    continue;
                }

                if (m_onlySerializeExplicitFields && !fld.ExplicitlySerialize)
                {
                    // the field is NOT explicitly marked AND the class requests ONLY explicitly marked fields get serialized
                    continue;
                }

                m_fields.Add(fld);
            }
        }
        /// <summary>
        /// Use the data found in the recursion state (this object) to serialize a single
        /// element in the array to Xml.
        /// </summary>
        private void AddArrayElementToXml()
        {
            var obj = m_array.GetValue(m_indicies);

            // the m_indicies array happens to be in the format needed for Array.GetValue

            if (SimpleElementSerialization)
            {
                if (m_arrayType.IsPrimitive)
                {
                    m_simpleElements.Append(obj.ToString());
                }
                else // its a string
                {
                    var preEscaped = obj as string;
                    var escaped    = CFramework.ProtectStringForStringlist(preEscaped);
                    m_simpleElements.Append(escaped);
                }
                m_simpleElements.Append(",");
            }
            else if (obj == null && m_serializer.Context.RemoveNullValuesFromXml)
            {
                m_skippedNull = true;
            }
            else
            {
                var elem =
                    m_serializer.FrameworkSerialize(m_elementName,
                                                    obj,
                                                    m_xmlToAddTo,
                                                    m_arrayType);
                if (m_serializer.Context.ArrayElementsIncludeIndicies || m_skippedNull)
                {
                    elem.AddAttribute(m_serializer.Context.ArrayIndexAttributeName,
                                      Lib.ConvertArrayToString(m_indicies));
                }
                m_skippedNull = false;
            }
        }