Ejemplo n.º 1
0
        private bool DoWriteInlineObjectStructured(object o, Type t, bool writeTypeInfo)
        {
            Debug.Assert(o != null);
            Type   serializerServiceType = typeof(IStructuredSerializer <>).MakeGenericType(t);
            object serializer            = GetService(serializerServiceType);

            if (serializer != null)
            {
                using (ISubStructuredWriter wSub = OpenSubWriter())
                {
                    BindingFlags fInvoke    = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod;
                    object[]     parameters = new object[] { wSub, o };
                    if (writeTypeInfo)
                    {
                        Xml.WriteAttributeString("type", "Structured");
                        Xml.WriteAttributeString("typeName", GetTypeName(t));
                    }
                    serializerServiceType.InvokeMember("WriteInlineContent", fInvoke, null, serializer, parameters);
                    DoWriteEnd(wSub, o);
                }
                return(true);
            }
            IStructuredSerializable structured = o as IStructuredSerializable;

            if (structured != null)
            {
                using (ISubStructuredWriter wSub = OpenSubWriter())
                {
                    if (writeTypeInfo)
                    {
                        Xml.WriteAttributeString("type", "Structured");
                        Xml.WriteAttributeString("typeName", GetTypeName(t));
                    }
                    structured.WriteContent(wSub);
                    DoWriteEnd(wSub, o);
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        void IStructuredWriter.WriteInlineObject(object o)
        {
            AssertCurrentWriter();

            if (o == null)
            {
                Xml.WriteAttributeString("type", "null");
                Xml.WriteEndElement();
                return;
            }

            Type t = o.GetType();

            if (DoWriteInlineObjectStructured(o, t, true))
            {
                return;
            }
            if (t.IsValueType)
            {
                if (t.IsEnum)
                {
                    Xml.WriteAttributeString("type", "Enum");
                    Xml.WriteAttributeString("typeName", GetTypeName(t));
                    Xml.WriteString(Enum.Format(t, o, "g"));
                }
                else if (t == typeof(Int32))
                {
                    WriteTypedObjectContent(Xml, (Int32)o);
                }
                else if (t == typeof(bool))
                {
                    WriteTypedObjectContent(Xml, (bool)o);
                }
                else if (t == typeof(Guid))
                {
                    WriteTypedObjectContent(Xml, (Guid)o);
                }
                else if (t == typeof(char))
                {
                    WriteTypedObjectContent(Xml, (char)o);
                }
                else if (t == typeof(Decimal))
                {
                    WriteTypedObjectContent(Xml, (Decimal)o);
                }
                else if (t == typeof(Single))
                {
                    WriteTypedObjectContent(Xml, (Single)o);
                }
                else if (t == typeof(Double))
                {
                    WriteTypedObjectContent(Xml, (Double)o);
                }
                else if (t == typeof(UInt32))
                {
                    WriteTypedObjectContent(Xml, (UInt32)o);
                }
                else if (t == typeof(Int64))
                {
                    WriteTypedObjectContent(Xml, (Int64)o);
                }
                else if (t == typeof(UInt64))
                {
                    WriteTypedObject(Xml, (UInt64)o);
                }
                else if (t == typeof(Int16))
                {
                    WriteTypedObjectContent(Xml, (Int16)o);
                }
                else if (t == typeof(UInt16))
                {
                    WriteTypedObjectContent(Xml, (UInt16)o);
                }
                else if (t == typeof(SByte))
                {
                    WriteTypedObjectContent(Xml, (SByte)o);
                }
                else if (t == typeof(Byte))
                {
                    WriteTypedObjectContent(Xml, (Byte)o);
                }
                else if (t == typeof(DateTime))
                {
                    WriteTypedObjectContent(Xml, (DateTime)o);
                }
                else if (t == typeof(TimeSpan))
                {
                    WriteTypedObject(Xml, (TimeSpan)o);
                }
                else if (t.IsSerializable)
                {
                    WriteSerializable(Xml, o);
                }
                else
                {
                    throw new CKException("Unable to Serialize object of Type {0} from assembly {1}.", t.FullName, t.Assembly.FullName);
                }
                Xml.WriteEndElement();
                return;
            }
            if (t == typeof(string))
            {
                WriteTypedObjectContent(Xml, (string)o);
                Xml.WriteEndElement();
                return;
            }
            if (t == typeof(CultureInfo))
            {
                WriteTypedObjectContent(Xml, (CultureInfo)o);
                Xml.WriteEndElement();
                return;
            }
            using (ISubStructuredWriter wSub = OpenSubWriter())
            {
                Debug.Assert(Root.Current == wSub);
                // IXmlSerializable or IsSerializable (reference type).
                IXmlSerializable xmlSer = o as IXmlSerializable;
                if (xmlSer != null)
                {
                    Xml.WriteAttributeString("type", "XmlSerializable");
                    Xml.WriteAttributeString("typeName", GetTypeName(t));
                    xmlSer.WriteXml(Xml);
                }
                else if (t.IsSerializable)
                {
                    WriteSerializable(Xml, o);
                }
                else
                {
                    throw new CKException("Unable to Serialize object of Type {0} from assembly {1}.", t.FullName, t.Assembly.FullName);
                }
                DoWriteEnd(wSub, o);
            }
        }