Ejemplo n.º 1
0
        void WriteValueContents(Type knownType, object value)
        {
            if (value == null)
            {
                CurrentGroup.Add(new XAttribute("type", "null"));
            }
            else
            {
                Type type = value.GetType();

                if (type.DerivesFrom <Type>())
                {
                    // Treat RuntimeType as if it's Type
                    type = typeof(Type);
                }

                var serializer = SerializerRegistry.GetSerializer(type);
                serializer.SerializeConstructor(ref value, this);
                serializer.SerializeContents(ref value, this);
                serializer.SerializeBacklinks(ref value, this);

                if (type != knownType)
                {
                    CurrentGroup.Add(new XAttribute("type", SerializerRegistry.GetSerializedTypeName(type)));
                }
            }
        }
Ejemplo n.º 2
0
        public void IncludeReference(object value)
        {
            if (value == null)
            {
                return;
            }

            Type type = value.GetType();

            bool firstTime;
            long id = _objectIdGenerator.GetId(value, out firstTime);

            if (!_lookup.ContainsKey(id))
            {
                XElement rootElement = new XElement("Reference");
                rootElement.Add(new XAttribute("id", id));
                rootElement.Add(new XAttribute("type", SerializerRegistry.GetSerializedTypeName(type)));
                _lookup.Add(id, rootElement);
                _references.Add(id, value);
                _pending.Enqueue(id);
            }
        }
Ejemplo n.º 3
0
        public void WritePrimitive(Type knownType, object value)
        {
            Assert.IsNotNull(knownType);
            Assert.IsNotNull(value);

            if (knownType == typeof(bool))
            {
                CurrentGroup.Value = value.ToString();
            }
            else if (knownType == typeof(byte))
            {
                CurrentGroup.Value = value.ToString();
            }
            else if (knownType == typeof(short))
            {
                CurrentGroup.Value = value.ToString();
            }
            else if (knownType == typeof(ushort))
            {
                CurrentGroup.Value = value.ToString();
            }
            else if (knownType == typeof(int))
            {
                CurrentGroup.Value = value.ToString();
            }
            else if (knownType == typeof(uint))
            {
                CurrentGroup.Value = value.ToString();
            }
            else if (knownType == typeof(long))
            {
                CurrentGroup.Value = value.ToString();
            }
            else if (knownType == typeof(ulong))
            {
                CurrentGroup.Value = value.ToString();
            }
            else if (knownType == typeof(float))
            {
                CurrentGroup.Value = ((float)value).ToString("R");
            }
            else if (knownType == typeof(double))
            {
                CurrentGroup.Value = ((double)value).ToString("R");
            }
            else if (knownType == typeof(decimal))
            {
                CurrentGroup.Value = value.ToString();
            }
            else if (knownType == typeof(char))
            {
                CurrentGroup.Value = value.ToString();
            }
            else if (knownType == typeof(string))
            {
                CurrentGroup.Value = (string)value;
            }
            else if (knownType == typeof(byte[]))
            {
                CurrentGroup.Value = Convert.ToBase64String((byte[])value);
            }
            else if (knownType == typeof(Type))
            {
                CurrentGroup.Value = SerializerRegistry.GetSerializedTypeName((Type)value);
            }
            else if (knownType.IsEnum())
            {
                CurrentGroup.Value = value.ToString();
            }
            else
            {
                throw Assert.CreateException(
                          "unhandled primative type '{0}'", knownType.Name());
            }
        }