Exemplo n.º 1
0
 /// <summary>
 /// Reads the double property.
 /// </summary>
 /// <param name="name">Name of property.</param>
 /// <returns>Property value.</returns>
 public double ReadDouble(string name)
 {
     return((double)Converter.FromString(typeof(double), PropValue(name)));
 }
Exemplo n.º 2
0
        private void DoReadProperties(object obj, XmlProperty[] properties)
        {
            for (int i = 0; i < properties.Length; i++)
            {
                string name  = properties[i].Key;
                string value = properties[i].Value;

                // check multiple properties like Frame.LeftLine.Typ
                object obj1  = obj;
                int    len   = name.Length;
                int    start = 0;
                int    j     = 0;
                // find '.'
                while (j < len && name[j] != '.')
                {
                    j++;
                }
                if (j < len)
                {
                    while (j < len)
                    {
                        // get subproperty
                        PropertyInfo pi = obj1.GetType().GetProperty(name.Substring(start, j - start));
                        if (pi == null)
                        {
                            break;
                        }
                        obj1 = pi.GetValue(obj1, null);

                        // find next '.'
                        start = j + 1;
                        j++;
                        while (j < len && name[j] != '.')
                        {
                            j++;
                        }
                    }
                    name = name.Substring(start);
                }

                try
                {
                    name = PropName(name);
                    PropertyInfo pi = obj1.GetType().GetProperty(name);
                    if (pi == null)
                    {
                        continue;
                    }

                    if (value == "null")
                    {
                        if (pi.PropertyType == typeof(string))
                        {
                            pi.SetValue(obj1, "null", null);
                        }
                        else
                        {
                            pi.SetValue(obj1, null, null);
                        }
                    }
                    else
                    {
                        if (pi.PropertyType == typeof(string))
                        {
                            pi.SetValue(obj1, value, null);
                        }
                        else if (pi.PropertyType.IsClass && pi.PropertyType.IsSubclassOf(typeof(Base)))
                        {
                            // it's a reference
                            fixups.Add(new FixupInfo(obj1 as Base, name, value));
                        }
                        else
                        {
                            pi.SetValue(obj1, Converter.FromString(pi.PropertyType, value), null);
                        }
                    }
                }
                catch (Exception e)
                {
                    errors += e.Message + "\r\n";
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Reads the float property.
 /// </summary>
 /// <param name="name">Name of property.</param>
 /// <returns>Property value.</returns>
 public float ReadFloat(string name)
 {
     return((float)Converter.FromString(typeof(float), PropValue(name)));
 }
Exemplo n.º 4
0
        /// <summary>
        /// Reads properties of specified object.
        /// </summary>
        /// <param name="obj">The object to read.</param>
        /// <remarks>
        /// This method reads simple properties like "Text", "Border.Lines" etc. for specified object.
        /// To read nested properties like collections, you should override the <see cref="Base.DeserializeSubItems"/>
        /// method of an object.
        /// </remarks>
        /// <example>This example demonstrates the use of <b>ReadProperties</b>, <b>ReadChildren</b>,
        /// <b>NextItem</b>, <b>Read</b> methods.
        /// <code>
        /// public void Deserialize(FRReader reader)
        /// {
        ///   // read simple properties like "Text", complex properties like "Border.Lines"
        ///   reader.ReadProperties(this);
        ///
        ///   // moves the current reader item
        ///   while (reader.NextItem())
        ///   {
        ///     // read the "Styles" collection
        ///     if (String.Compare(reader.ItemName, "Styles", true) == 0)
        ///       reader.Read(Styles);
        ///     else if (reader.ReadChildren)
        ///     {
        ///       // if read of children is enabled, read them
        ///       Base obj = reader.Read();
        ///       if (obj != null)
        ///          obj.Parent = this;
        ///     }
        ///   }
        /// }
        /// </code>
        /// </example>
        public void ReadProperties(object obj)
        {
            // speed optimization, for use in the preview mode
            if (obj is TextObject && FProps.Count == 1 && FProps[0].Name == "x")
            {
                (obj as TextObject).Text = Converter.FromXml(FProps[0].Value);
                return;
            }

            for (int i = 0; i < FProps.Count; i++)
            {
                string name  = FProps[i].Name;
                string value = FProps[i].Value;

                // check multiple properties like Frame.LeftLine.Typ
                object obj1  = obj;
                int    len   = name.Length;
                int    start = 0;
                int    j     = 0;
                // find '.'
                while (j < len && name[j] != '.')
                {
                    j++;
                }
                if (j < len)
                {
                    while (j < len)
                    {
                        // get subproperty
                        PropertyInfo pi = obj1.GetType().GetProperty(name.Substring(start, j - start));
                        if (pi == null)
                        {
                            break;
                        }
                        obj1 = pi.GetValue(obj1, null);

                        // find next '.'
                        start = j + 1;
                        j++;
                        while (j < len && name[j] != '.')
                        {
                            j++;
                        }
                    }
                    name = name.Substring(start);
                }

                try
                {
                    name = PropName(name);
                    PropertyInfo pi = obj1.GetType().GetProperty(name);
                    if (pi == null)
                    {
                        continue;
                    }

                    if (value == "null")
                    {
                        pi.SetValue(obj1, value, null);
                    }
                    else
                    {
                        if (pi.PropertyType == typeof(string))
                        {
                            pi.SetValue(obj1, Converter.FromXml(value), null);
                        }
                        else if (pi.PropertyType.IsClass && pi.PropertyType.IsSubclassOf(typeof(Base)))
                        {
                            // it's a reference
                            FFixups.Add(new FixupInfo(obj1 as Base, name, Converter.FromXml(value)));
                        }
                        else
                        {
                            pi.SetValue(obj1, Converter.FromString(pi.PropertyType, Converter.FromXml(value)), null);
                        }
                    }
                }
                catch (Exception e)
                {
                    FErrors += e.Message + "\r\n";
                }
            }
        }