Exemple #1
0
        public ParamValueCollection GetParamValues(string prefix = "")
        {
            var collection = new ParamValueCollection(prefix);

            collection.Add("First", this.First);
            collection.Add("Last", this.Last);
            collection.Add("Second", this.Second);

            return(collection);
        }
Exemple #2
0
        static public ParamValueCollection GetObjectProperties(object obj, string prefix = "")
        {
            var result = new ParamValueCollection(prefix);

            if (obj == null)
            {
                return(result);
            }

            var properties = obj.GetType().GetProperties();

            foreach (var p in properties)
            {
                string name  = p.Name;
                var    value = p.GetValue(obj, null);

                if (value == null)
                {
                    continue;
                }

                result.Add(GetParamValues(name, value));
            }

            return(result);
        }
Exemple #3
0
        static public ParamValueCollection GetParamValues(string name, object value)
        {
            var result = new ParamValueCollection();

            if (value == null)
            {
                return(result);
            }

            var type = value.GetType();

            if (type == typeof(string))
            {
                result.Add(name, value.ToString());
            }
            else if (type == typeof(DateTime))
            {
                result.Add(name, ((DateTime)value));
            }
            else if (type == typeof(DateTimeOffset))
            {
                result.Add(name, ((DateTimeOffset)value).DateTime);
            }
            else if (type.IsClass)
            {
                if (!typeof(IEnumerable).IsAssignableFrom(type) && !type.IsArray)
                {
                    result.Add(GetObjectProperties(value, name));
                }
            }
            else
            {
                result.Add(name, value.ToString());
            }

            return(result);
        }
Exemple #4
0
        public static ParamValueCollection Split(string text, params char[] separators)
        {
            var result = new ParamValueCollection();

            if (String.IsNullOrWhiteSpace(text))
            {
                return(result);
            }

            var items = text.SplitFormatted(separators);

            foreach (var item in items)
            {
                if (String.IsNullOrWhiteSpace(item))
                {
                    continue;
                }

                var split = item.Split('=');
                if (split == null || split.Length != 2)
                {
                    throw new CustomFormatException("Param's Name and Value should be separated by '='");
                }

                var name = split[0].Trim();
                if (name.Length > 2 && name[0] == name[name.Length - 1] && separators.Contains(name[0]))
                {
                    name = name.Substring(1, name.Length - 2);
                }

                var value = split[1].Trim();
                if (value.Length > 2 && value[0] == value[value.Length - 1] && separators.Contains(value[0]))
                {
                    value = value.Substring(1, value.Length - 2);
                }

                result.Add(name, value);
            }

            return(result);
        }