Пример #1
0
        private void ToXmlAs(IMarshalContext context, Type type, object value, XmlTextWriter xml)
        {
            // Get all the fields of the object
            FieldInfo[] fields = GetFields(type, value);

            // Serialize all fields
            foreach (FieldInfo objectField in fields)
            {
                if (ShouldIgnore(objectField, context.IgnoredAttributeType))
                {
                    AddNullValue(objectField, xml);
                    continue;
                }
                try
                {
                    object fieldValue = objectField.GetValue(value);
                    if (fieldValue != null &&
                        fieldValue.GetType().Name.StartsWith("CProxyType") &&
                        !fieldValue.GetType().Name.Contains("Hibernate"))
                    {
                        objectField.SetValue(value, null, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance |
                                             BindingFlags.DeclaredOnly, null, null);
                        fieldValue = null;
                    }

                    if (fieldValue != null)
                    {
                        IConverter converter = null;
                        if (fieldValue.GetType() == typeof(string) && context.IsCData(type, MarshalContext.auto_property_name(objectField.Name)))
                        {
                            converter = context.GetCDataConverter();
                        }
                        else
                        {
                            converter = context.GetConverter(fieldValue.GetType());
                        }
                        converter.ToXml(fieldValue, objectField, xml, context);
                    }
                    else
                    {
                        AddNullValue(objectField, xml);
                    }
                }
                catch (Exception e)
                {
                    throw new ApplicationException("Couldn't set field " + objectField.Name + " in object " + value + ": " + e.Message, e);
                }
            }
            if (type != typeof(object))
            {
                ToXmlAs(context, type.BaseType, value, xml);
            }
        }
Пример #2
0
        private void ToXmlAs(IMarshalContext context, Type type, object value, XmlTextWriter xml)
        {
            // Get all the fields of the object
            FieldInfo[] fields = GetFields(type, value);

            // Serialize all fields
            foreach (FieldInfo objectField in fields)
            {
                if (ShouldIgnore(objectField, context.IgnoredAttributeType))
                {
                    AddNullValue(objectField, xml);
                    continue;
                }
                try
                {
                    object fieldValue = objectField.GetValue(value);
                    if (fieldValue != null 
                        && fieldValue.GetType().Name.StartsWith("CProxyType") 
                        && !fieldValue.GetType().Name.Contains("Hibernate"))
                    {
                        objectField.SetValue(value, null, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance |
                                                          BindingFlags.DeclaredOnly, null, null);
                        fieldValue = null;
                    }

                    if (fieldValue != null)
                    {
                        IConverter converter = null;
                        if (fieldValue.GetType() == typeof(string) && context.IsCData(type, MarshalContext.auto_property_name(objectField.Name)))
                            converter = context.GetCDataConverter();
                        else
                            converter = context.GetConverter(fieldValue.GetType());
                        converter.ToXml(fieldValue, objectField, xml, context);
                    }
                    else
                        AddNullValue(objectField, xml);
                }
                catch (Exception e)
                {
                    throw new ApplicationException("Couldn't set field " + objectField.Name + " in object " + value + ": " + e.Message, e);
                }
            }
            if (type != typeof (object))
                ToXmlAs(context, type.BaseType, value, xml);
        }