public void Write(TextWriter textWriter)
        {
            string typeName;
            Type   type = null;

            if (Value != null)
            {
                type     = Value.GetType();
                typeName = TypeNameCollection.GetTypeName(type);
            }
            else
            {
                typeName = "object";
            }

            textWriter.Write("  " + typeName + " " + Name + " = ");

            if (type != null)
            {
                if (type.IsArray)
                {
                    var elementType = type.GetElementType();

                    if (elementType == typeof(byte))
                    {
                        var inArray = (byte[])Value;
                        var base64  = System.Convert.ToBase64String(inArray);
                        textWriter.WriteLine(base64);
                    }
                    else
                    {
                        var array = (Array)Value;

                        if (array.Length > 0)
                        {
                            textWriter.WriteLine();

                            var index = 0;

                            foreach (var arrayItem in array)
                            {
                                textWriter.Write("    [" + index + "] = ");
                                Write(arrayItem, textWriter);
                                index++;
                            }
                        }
                    }
                }
                else
                {
                    Write(Value, textWriter);
                }
            }
            else
            {
                Write(Value, textWriter);
            }
        }
Пример #2
0
        private static Type GetType(string typeName)
        {
            Type type;

            if (typeName == null)
            {
                type = typeof(string);
            }
            else
            {
                type = Type.GetType(typeName);

                if (type == null)
                {
                    type = TypeNameCollection.GetType(typeName);
                }
            }

            return(type);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="xmlWriter"></param>
        /// <param name="attributes"></param>
        public static void Write(XmlWriter xmlWriter, ConfigurationAttributeCollection attributes)
        {
            foreach (var attribute in attributes)
            {
                using (xmlWriter.WriteElement(ConfigurationElementName.Attribute))
                {
                    xmlWriter.WriteAttributeString("name", attribute.Name);
                    var value = attribute.Value;

                    if (value != null)
                    {
                        var type = value.GetType();

                        if (type != typeof(string))
                        {
                            var typeName = TypeNameCollection.GetTypeName(type);
                            xmlWriter.WriteAttributeString("type", typeName);
                        }

                        var    typeCode = Type.GetTypeCode(type);
                        string strValue;

                        switch (typeCode)
                        {
                        case TypeCode.Object:
                            if (type == typeof(TimeSpan))
                            {
                                var timeSpan = (TimeSpan)value;
                                strValue = timeSpan.ToString();
                                xmlWriter.WriteAttributeString("value", strValue);
                            }
                            else if (type.IsArray)
                            {
                                var array = (Array)value;

                                for (var j = 0; j < array.Length; j++)
                                {
                                    using (xmlWriter.WriteElement("a"))
                                    {
                                        value    = array.GetValue(j);
                                        strValue = value.ToString();
                                        xmlWriter.WriteAttributeString("value", strValue);
                                    }
                                }
                            }
                            else
                            {
                                var xmlSerializer = new XmlSerializer(type);
                                xmlSerializer.Serialize(xmlWriter, value);
                            }

                            break;

                        default:
                            strValue = value.ToString();
                            xmlWriter.WriteAttributeString("value", strValue);
                            break;
                        }
                    }
                    else
                    {
                        xmlWriter.WriteAttributeString("isNull", bool.TrueString);
                    }
                }
            }
        }