Exemplo n.º 1
0
        /// <summary>
        /// Loads container of services.
        /// </summary>
        /// <param name="tr">XmlReader.</param>
        public void Load(XmlReader tr)
        {
            var        converter = new StiObjectStringConverter();
            StiService service   = null;

            tr.Read();
            while (tr.Read())
            {
                if (tr.IsStartElement())
                {
                    #region service
                    if (tr.Name == "service")
                    {
                        string assembly    = tr.GetAttribute("assembly");
                        string serviceType = tr.GetAttribute("type");

                        if (assemlyToSkip[assembly.ToLower(System.Globalization.CultureInfo.InvariantCulture)] == null)
                        {
                            try
                            {
                                service = CreateService(assembly, serviceType);
                                if (service != null)
                                {
                                    Add(service, false);
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    #endregion

                    #region property
                    else if (tr.Name == "property" && service != null)
                    {
                        string propName = tr.GetAttribute("name");
                        string typeStr  = tr.GetAttribute("type");
                        var    propInfo = service.GetType().GetProperty(propName);

                        if (propInfo != null)
                        {
                            if (tr.GetAttribute("isNull") != null)
                            {
                                propInfo.SetValue(service, null, null);
                            }
                            else if (tr.GetAttribute("isList") != null)
                            {
                                int   count       = int.Parse(tr.GetAttribute("count"));
                                Type  elementType = StiTypeFinder.GetType(typeStr);
                                Array list        = Array.CreateInstance(elementType, count);

                                int index = 0;
                                while (tr.Read())
                                {
                                    if (tr.IsStartElement())
                                    {
                                        string nm = tr.Name;

                                        string valueStr = tr.ReadString();
                                        object value    = converter.StringToObject(valueStr, elementType);
                                        if (value != null)
                                        {
                                            list.SetValue(value, new int[] { index++ });
                                        }
                                        if (index >= count)
                                        {
                                            break;
                                        }
                                    }
                                }
                                propInfo.SetValue(service, list, null);
                            }
                            else
                            {
                                string valueStr = tr.GetAttribute("value");
                                object value    = converter.StringToObject(valueStr, StiTypeFinder.GetType(typeStr));
                                if (value != null)
                                {
                                    propInfo.SetValue(service, value, null);
                                }
                            }
                        }
                    }
                    #endregion
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Saves container of services.
        /// </summary>
        /// <param name="tw">XmlWriter.</param>
        public void Save(XmlWriter tw)
        {
            StiObjectStringConverter converter = new StiObjectStringConverter();

            tw.WriteStartElement("Services");

            foreach (StiService service in this)
            {
                tw.WriteStartElement("service");

                Assembly a    = service.GetType().Assembly;
                string   path = Path.GetExtension(a.Location);

                tw.WriteAttributeString("assembly", a.GetName().Name + path);
                tw.WriteAttributeString("type", service.GetType().ToString());

                #region Serialize service parameters
                PropertyDescriptorCollection props = TypeDescriptor.GetProperties(service);

                if (props != null && props.Count != 0)
                {
                    props = props.Sort();

                    foreach (PropertyDescriptor prop in props)
                    {
                        StiServiceParamAttribute param =
                            prop.Attributes[typeof(StiServiceParamAttribute)] as StiServiceParamAttribute;

                        if (param != null)
                        {
                            DefaultValueAttribute defaultAttr =
                                prop.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;

                            object value = prop.GetValue(service);

                            if (defaultAttr == null || (!defaultAttr.Value.Equals(value)))
                            {
                                tw.WriteStartElement("property");

                                #region Null
                                if (value == null)
                                {
                                    tw.WriteAttributeString("isNull", "True");
                                }
                                #endregion

                                #region isList
                                else if (value is Array)
                                {
                                    tw.WriteAttributeString("name", prop.Name);
                                    tw.WriteAttributeString("isList", "True");
                                    tw.WriteAttributeString("count", ((Array)value).Length.ToString());
                                    tw.WriteAttributeString("type", value.GetType().GetElementType().ToString());

                                    Array array = value as Array;
                                    foreach (object val in array)
                                    {
                                        tw.WriteStartElement("item");
                                        tw.WriteString(converter.ObjectToString(val));
                                        tw.WriteEndElement();
                                    }
                                }
                                #endregion

                                #region Simple
                                else
                                {
                                    tw.WriteAttributeString("name", prop.Name);
                                    tw.WriteAttributeString("type", value.GetType().ToString());
                                    tw.WriteAttributeString("value", converter.ObjectToString(value));
                                }
                                #endregion

                                tw.WriteEndElement();
                            }
                        }
                        param = null;
                    }
                }
                #endregion

                tw.WriteEndElement();
            }

            tw.WriteEndElement();
        }