Esempio n. 1
0
        private static void WriteIDictionary(TextWriter writer, IDictionary map,
                                             QueryStringTypeSerializerSettings settings)
        {
            var serializer = new QueryStringTypeSerializer(settings);
            var ranOnce    = false;

            foreach (var key in map.Keys)
            {
                var dictionaryValue = map[key];
                if (dictionaryValue == null)
                {
                    continue;
                }

                if (ranOnce)
                {
                    writer.Write("&");
                }
                else
                {
                    ranOnce = true;
                }

                WriteValue(serializer, writer, key);
                writer.Write("=");
                WriteValue(serializer, writer, dictionaryValue);
            }
        }
Esempio n. 2
0
        public static string SerializeObject(object value, QueryStringTypeSerializerSettings settings)
        {
            var type = value.GetType();

            if (type.IsAssignableFrom(typeof(IDictionary)) || type.HasInterface(typeof(IDictionary)))
            {
                using (var writer = new StringWriter())
                {
                    WriteIDictionary(writer, (IDictionary)value, settings);
                    return(writer.ToString());
                }
            }

            var isEnumerable = type.IsAssignableFrom(typeof(IEnumerable)) || type.HasInterface(typeof(IEnumerable));

            if (isEnumerable)
            {
                throw new NotSupportedException("不支持可枚举类型的序列化");
            }
            if (type.GetTypeInfo().IsClass || type.GetTypeInfo().IsInterface)
            {
                using (var writer = new StringWriter())
                {
                    WriteType(writer, value, settings);
                    return(writer.ToString());
                }
            }

            throw new NotSupportedException("不支持的对象类型");
        }
Esempio n. 3
0
        private static void WriteType(TextWriter writer, object value, QueryStringTypeSerializerSettings settings)
        {
            var type       = value.GetType();
            var props      = type.GetSerializableProperties();
            var serializer = new QueryStringTypeSerializer(settings);

            for (var i = 0; i < props.Length; i++)
            {
                var pi     = props[i];
                var pValue = pi.GetValue(value);
                if (pValue == null)
                {
                    continue;
                }

                if (i > 0)
                {
                    writer.Write('&');
                }

                writer.Write(GetPropertyName(pi));
                writer.Write("=");

                WriteValue(serializer, writer, pValue);
            }
        }
Esempio n. 4
0
 public QueryStringTypeSerializer(QueryStringTypeSerializerSettings settings)
 {
     Settings = settings;
 }