Beispiel #1
0
        public static string ToUrlParams <T>(T data)
        {
            Type tp = typeof(T);

            if (null == data)
            {
                throw new Exception("Invalid arg!");
            }

            StringBuilder sb = new StringBuilder();

            foreach (FieldInfo field in tp.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
            {
                sb.Append('&');
                Type fieldType = field.FieldType;

                if (fieldType.IsValueType)
                {
                    sb.Append(FieldNameFilter(fieldType.Name) + "=" + SimpleJsonSerializer.StringifyValue(field.GetValue(data)));
                }
                else if (fieldType.Equals(typeof(string)))
                {
                    sb.Append(FieldNameFilter(fieldType.Name) + "=" + SimpleJsonSerializer.StringifyString(field.GetValue(data)));
                }
                else if (fieldType.IsEnum)
                {
                    sb.Append(FieldNameFilter(fieldType.Name) + "=" + SimpleJsonSerializer.StringifyEnum(field.GetValue(data)));
                }
                else
                {
                    throw new Exception("Not supported! " + tp.FullName);
                }
            }

            return(sb.ToString());
        }