Example #1
0
        public void CodeFields(Type type, ITypedMap obj)
        {
            TypeOfString fieldTypeMap = FieldTypeMap.Get(type, obj);

            int fieldCount = m_reader.ReadInt32();

            while (fieldCount-- > 0)
            {
                string fieldName = m_reader.ReadString();
                if ((m_debugMode & CoderDebugMode.ReportFields) != 0)
                {
                    Report.Line("  {0}", fieldName);
                }
                object value            = null;
                bool   directlyCodeable = m_reader.ReadBoolean();
                if (directlyCodeable)
                {
                    Type fieldType;
                    if (!fieldTypeMap.TryGetValue(fieldName, out fieldType))
                    {
                        throw new InvalidOperationException(String.Format("Could not get type of field \"{0}\"", fieldName));
                    }
                    // this can happen if a SymMap is used and a field has been removed
                    TypeCoder.Read(this, fieldType, ref value);
                }
                else
                {
                    Code(ref value);
                }
                obj[fieldName] = value;
            }
        }
        public void CodeFields(Type type, ITypedMap obj)
        {
            TypeOfString fieldTypeMap = FieldTypeMap.Get(type, obj);

            foreach (string fieldName in obj.FieldNames)
            {
                XElement   element   = null;
                XAttribute attribute = null;
                object     value     = obj[fieldName];
                Type       fieldType;
                string     fieldTypeName = null;
                if (!fieldTypeMap.TryGetValue(fieldName, out fieldType))
                {
                    TypeInfo typeInfo;
                    fieldType = value.GetType();
                    if (TryGetTypeInfo(fieldType, out typeInfo))
                    {
                        fieldTypeName = typeInfo.XmlName;
                    }
                    else
                    {
                        fieldTypeName = fieldType.AssemblyQualifiedName;
                    }
                    attribute = new XAttribute("type", fieldTypeName);
                }
                else
                {
                    fieldTypeName = fieldType.Name;
                }

                if (fieldTypeName != fieldName || TypeCoder.IsDirectlyCodeable(fieldType))
                {
                    element = new XElement(fieldName);
                    if (attribute != null)
                    {
                        element.Add(attribute);
                    }
                    m_elementStack.Push(m_element);
                    m_element = element;
                }
                TypeCoder.Write(this, fieldType, ref value);
                if (fieldTypeName != fieldName || TypeCoder.IsDirectlyCodeable(fieldType))
                {
                    m_element = m_elementStack.Pop();
                    AddValue(element);
                }
            }
        }
        public void CodeFields(Type type, ITypedMap obj)
        {
            TypeOfString fieldTypeMap = FieldTypeMap.Get(type, obj);

            m_writer.Write(obj.FieldCount);

            foreach (string fieldName in obj.FieldNames)
            {
                m_writer.Write(fieldName);
                object value            = obj[fieldName];
                bool   directlyCodeable = true;
                Type   fieldType        = null;

                if ((m_debugMode & CoderDebugMode.ReportFields) != 0)
                {
                    Report.Line("  {0}", fieldName);
                }

                // A field is directly codeable, if its type information can
                // be computed by the writer and the reader without
                // inspecting the actual field value.

                if (!fieldTypeMap.TryGetValue(fieldName, out fieldType) ||
                    (value != null && fieldType != value.GetType()) ||
                    !TypeCoder.IsDirectlyCodeable(fieldType))
                {
                    directlyCodeable = false;
                }

                // We store the directly codeable flag, so that a stored file
                // can be read, even if the class has been modified, and the
                // value of directly codeable has been changed.

                m_writer.Write(directlyCodeable);
                if (directlyCodeable)
                {
                    TypeCoder.Write(this, fieldType, ref value);
                }
                else
                {
                    Code(ref value);
                }
            }
        }