Esempio n. 1
0
        /// Generic function to read an object value.  Returns true if the read
        /// succeeded.
        private bool ReadValue(XmlNode node, TypeConverter converter, out object value)
        {
            try
            {
                foreach (XmlNode child in node.ChildNodes)
                {
                    if (child.NodeType == XmlNodeType.Text)
                    {
                        value = converter.ConvertFromInvariantString(node.InnerText);
                        return(true);
                    }
                    else if (child.Name.Equals("Binary"))
                    {
                        byte[] data = Convert.FromBase64String(child.InnerText);


                        // Binary blob.  Now, check to see if the type converter
                        // can convert it.  If not, use serialization.
                        //
                        if (GetConversionSupported(converter, typeof(byte[])))
                        {
                            value = converter.ConvertFrom(null, CultureInfo.InvariantCulture, data);

                            return(true);
                        }
                        else
                        {
                            BinaryFormatter formatter = new BinaryFormatter();
                            MemoryStream    stream    = new MemoryStream(data);

                            value = formatter.Deserialize(stream);
                            return(true);
                        }
                    }
                    else if (child.Name.Equals("InstanceDescriptor"))
                    {
                        value = ReadInstanceDescriptor(child);
                        return(value != null);
                    }
                    else if (child.Name.Equals("Reference"))
                    {
                        XmlAttribute memberAttr = child.Attributes["name"];

                        return(FindComponent(memberAttr.Value, out value));
                    }
                    else
                    {
                        //MessageBox.Show(string.Format("Unexpected element type {0}", child.Name),"错误17");
                        value = null;
                        return(false);
                    }
                }

                // If we get here, it is because there were no nodes.  No nodes and no inner
                // text is how we signify null.
                //
                value = null;
                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误18");
                value = null;
                return(false);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Конвертирует строку в указанный тип, используя инвариантную культуру.
        /// </summary>
        /// <typeparam name="T">Тип, в который будет сконвертирована строка.</typeparam>
        /// <param name="value">Строка.</param>
        /// <returns>Результат конвертации.</returns>
        private static T ConvertTo <T>(string value)
        {
            TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));

            return((T)converter.ConvertFromInvariantString(value));
        }