Esempio n. 1
0
        /// <summary> Serialization implementation. </summary>
        public override bool Serialize(XElement elem, object sourceObj, ITypeData expectedType)
        {
            if (sourceObj is IEnumerable == false || sourceObj is string)
            {
                return(false);
            }
            IEnumerable sourceEnumerable = (IEnumerable)sourceObj;
            Type        type             = sourceObj.GetType();
            Type        genericTypeArg   = type.GetEnumerableElementType();

            if (genericTypeArg.IsNumeric())
            {
                var parser = new NumberFormatter(CultureInfo.InvariantCulture)
                {
                    UseRanges = false
                };
                elem.Value = parser.FormatRange(sourceEnumerable);
                return(true);
            }

            object prevObj = this.Object;

            try
            {
                this.Object = sourceObj;
                bool isComponentSettings = sourceObj is ComponentSettings;
                foreach (object obj in sourceEnumerable)
                {
                    var step = new XElement(Element);
                    if (obj != null)
                    {
                        type      = obj.GetType();
                        step.Name = TapSerializer.TypeToXmlString(type);
                        if (isComponentSettings)
                        {
                            ComponentSettingsSerializing.Add(obj);
                        }
                        try
                        {
                            Serializer.Serialize(step, obj, expectedType: TypeData.FromType(genericTypeArg));
                        }
                        finally
                        {
                            if (isComponentSettings)
                            {
                                ComponentSettingsSerializing.Remove(obj);
                            }
                        }
                    }

                    elem.Add(step);
                }
            }
            finally
            {
                this.Object = prevObj;
            }

            return(true);
        }