예제 #1
0
        public static string ReplaceRequestParam <T>(ConverterCollection converters, string uri, string name, T value)
        {
            if (Type.GetTypeCode(typeof(T)) == TypeCode.Object)
            {
                var converter = converters.FindConverter <T, string>();
                if (converter != null)
                {
                    return(ReplaceRequestParam(uri, name, converter.Convert(value)));
                }

                // get properties

                foreach (var property in typeof(T).GetProperties())
                {
                    if (property.GetMethod == null)
                    {
                        continue;
                    }
                    object propertyValue = property.GetValue(value);
                    if (propertyValue == null)
                    {
                        continue;
                    }
                    if (propertyValue is string)
                    {
                        uri = ReplaceRequestParam(uri, property.Name, propertyValue.ToString());
                        continue;
                    }
                    if (propertyValue is IEnumerable)
                    {
                        foreach (var item in propertyValue as IEnumerable)
                        {
                            if (item == null)
                            {
                                continue;
                            }
                            uri = ReplaceRequestParam(uri, property.Name, converters.ConvertValue <string>(item, true));
                        }
                        continue;
                    }
                    uri = ReplaceRequestParam(uri, property.Name, converters.ConvertValue <string>(propertyValue, true));
                }

                return(uri);
            }

            return(ReplaceRequestParam(uri, name, converters.ConvertValue <T, string>(value, true)));
        }
        public static string ReplaceRequestQuery <T>(ConverterCollection converters, string uri, string name, T value)
        {
            var typeCode = Type.GetTypeCode(typeof(T));

            if (typeCode == TypeCode.Object)
            {
                if (value == null)
                {
                    return(uri);
                }
                //TODO: ReplaceRequestQuery
                //foreach (var property in value.GetType().GetProperties())
                //{
                //    object propertyValue = property.GetValue(value);
                //    if (propertyValue == null)
                //    {
                //        continue;
                //    }
                //    if (propertyValue is IEnumerable&&propertyValue)
                //    {

                //    }
                //}
                return(uri);
            }
            else
            {
                return(ReplaceRequestQuery(uri, name, converters.ConvertValue <T, string>(value, true)));
            }
        }
예제 #3
0
        public static string ReplaceRequestQuery <T>(ConverterCollection converters, NamingPolicy namingPolicy, string uri, string name, T value, bool urlEncode)
        {
            var typeCode = Type.GetTypeCode(typeof(T));

            if (typeCode == TypeCode.Object)
            {
                foreach (var item in GetObjectStringParameters(name, value, converters, namingPolicy))
                {
                    uri = ReplaceRequestQuery(uri, item.Key, item.Value, urlEncode);
                }
                return(uri);
            }
            else
            {
                return(ReplaceRequestQuery(uri, name, converters.ConvertValue <T, string>(value, true), urlEncode));
            }
        }
예제 #4
0
 public static string ReplacePathVariable <T>(ConverterCollection converters, string uri, string name, T value)
 {
     return(ReplacePathVariable(uri, name, converters.ConvertValue <T, string>(value, true)));
 }
예제 #5
0
        public static string ReplaceRequestQuery <T>(ConverterCollection converters, string uri, string name, T value)
        {
            var typeCode = Type.GetTypeCode(typeof(T));

            if (typeCode == TypeCode.Object)
            {
                if (value == null)
                {
                    return(uri);
                }
                //Nullable<>
                if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    return(ReplaceRequestQuery(uri, name, converters.ConvertValue <T, string>(value, true)));
                }

                if (typeof(IEnumerable).IsAssignableFrom(typeof(T)))
                {
                    foreach (var item in value as IEnumerable)
                    {
                        if (item == null)
                        {
                            continue;
                        }
                        uri = ReplaceRequestQuery(uri, name, converters.ConvertValue <string>(item, true));
                    }
                    return(uri);
                }

                //TODO: ReplaceRequestQuery
                //foreach (var property in value.GetType().GetProperties())
                //{
                //    object propertyValue = property.GetValue(value);
                //    if (propertyValue == null)
                //    {
                //        continue;
                //    }
                //    if (propertyValue is IEnumerable&&propertyValue)
                //    {

                //    }
                //}

                // get properties

                foreach (var property in typeof(T).GetProperties())
                {
                    if (property.GetMethod == null)
                    {
                        continue;
                    }
                    object propertyValue = property.GetValue(value);
                    if (propertyValue == null)
                    {
                        continue;
                    }
                    if (propertyValue is string)
                    {
                        uri = ReplaceRequestQuery(uri, property.Name, propertyValue.ToString());
                        continue;
                    }
                    if (propertyValue is IEnumerable)
                    {
                        foreach (var item in propertyValue as IEnumerable)
                        {
                            if (item == null)
                            {
                                continue;
                            }
                            uri = ReplaceRequestQuery(uri, property.Name, converters.ConvertValue <string>(item, true));
                        }
                        continue;
                    }
                    uri = ReplaceRequestQuery(uri, property.Name, converters.ConvertValue <string>(propertyValue, true));
                }

                return(uri);
            }
            else
            {
                return(ReplaceRequestQuery(uri, name, converters.ConvertValue <T, string>(value, true)));
            }
        }
예제 #6
0
        public static IEnumerable <KeyValuePair <string, string> > GetObjectStringParameters <T>(string name, T value, ConverterCollection converters, NamingPolicy namingPolicy)
        {
            if (value == null)
            {
                yield break;
            }
            //Nullable<>
            if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                yield return(new KeyValuePair <string, string>(name, converters.ConvertValue <T, string>(value, true)));

                yield break;
            }

            if (typeof(IDictionary).IsAssignableFrom(typeof(T)))
            {
                IDictionary map = ((IDictionary)value);
                foreach (var item in map.Keys)
                {
                    if (map[item] == null)
                    {
                        continue;
                    }
                    yield return(new KeyValuePair <string, string>(item.ToString(), converters.ConvertValue <string>(item, true)));
                }
                yield break;
            }

            if (typeof(IEnumerable).IsAssignableFrom(typeof(T)))
            {
                foreach (var item in value as IEnumerable)
                {
                    if (item == null)
                    {
                        continue;
                    }
                    yield return(new KeyValuePair <string, string>(name, converters.ConvertValue <string>(item, true)));
                }
                yield break;
            }

            //TODO: ReplaceRequestQuery
            //foreach (var property in value.GetType().GetProperties())
            //{
            //    object propertyValue = property.GetValue(value);
            //    if (propertyValue == null)
            //    {
            //        continue;
            //    }
            //    if (propertyValue is IEnumerable&&propertyValue)
            //    {

            //    }
            //}

            // get properties

            foreach (var property in typeof(T).GetProperties())
            {
                if (property.GetMethod == null)
                {
                    continue;
                }
                object propertyValue = property.GetValue(value);
                if (propertyValue == null)
                {
                    continue;
                }
                if (propertyValue is string)
                {
                    yield return(new KeyValuePair <string, string>(GetName(property, namingPolicy), propertyValue.ToString()));

                    continue;
                }
                if (propertyValue is IEnumerable)
                {
                    foreach (var item in propertyValue as IEnumerable)
                    {
                        if (item == null)
                        {
                            continue;
                        }
                        yield return(new KeyValuePair <string, string>(GetName(property, namingPolicy), converters.ConvertValue <string>(item, true)));
                    }
                    continue;
                }
                yield return(new KeyValuePair <string, string>(GetName(property, namingPolicy), converters.ConvertValue <string>(propertyValue, true)));
            }
        }