/// <summary>
            /// Format a List Of T
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="l"></param>
            /// <param name="format"></param>
            /// <param name="separator"></param>
            /// <param name="preFix"></param>
            /// <param name="postFix"></param>
            /// <returns></returns>
            public static string Format <T>(List <T> l, string format, string separator = ", ", string preFix = "", string postFix = "")
            {
                System.Text.StringBuilder b = new StringBuilder(1024);
                foreach (var e in l)
                {
                    b.AppendFormat(format, ExtendedFormat.FormatValue(e));
                    b.Append(separator);
                }
                string r = b.ToString();

                if (r.EndsWith(separator))
                {
                    r = r.Substring(0, r.Length - separator.Length);
                }
                return(preFix + r + postFix);
            }
            /// <summary>
            /// Format the content of a dictionary to a string
            /// </summary>
            /// <typeparam name="K">The type of the key</typeparam>
            /// <typeparam name="V">The type of the value</typeparam>
            /// <param name="dictionary">The dictionary to FormatString</param>
            /// <param name="format">The FormatString of each key and value. The default is "{0}:{1}"</param>
            /// <param name="separator">The separator between each key and value. The default is ", "</param>
            /// <param name="preFix">A string to output at the beginning of the string, the default is "{ "</param>
            /// <param name="postFix">A string to output at the end of the string, the default is " }"</param>
            /// <returns></returns>
            public static string Format <K, V>(IDictionary <K, V> dictionary, string format = "{0}:{1}", string separator = ", ", string preFix = "{ ", string postFix = " }")
            {
                if (dictionary == null)
                {
                    return("Null");
                }                                          // just in case

                System.Text.StringBuilder b = new StringBuilder(1024);
                b.Append(preFix);

                foreach (KeyValuePair <K, V> kv in dictionary)
                {
                    b.AppendFormat(format, ExtendedFormat.FormatValue(kv.Key, false), ExtendedFormat.FormatValue(kv.Value, true));
                    b.Append(separator);
                }
                b.Remove(b.Length - separator.Length, separator.Length); // Remove the separator
                b.Append(postFix);
                return(b.ToString());
            }
示例#3
0
 /// <summary>
 /// Format a value. A value here is the value of a parameter passed to a
 /// C# method.
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public static string FormatValue(object value, bool doubleQuoteIfNeeded = true)
 {
     if (value == null)
     {
         return("null");
     }
     else if (value == DBNull.Value)
     {
         return("DBNull");
     }
     else if (doubleQuoteIfNeeded && ExtendedFormat.IsTypeNeedDoubleQuote(value))
     {
         return(String.Format("\"{0}\"", value));
     }
     else
     {
         return(value.ToString());
     }
 }
示例#4
0
        /// <summary>
        /// Format a IDictionary of K,V to a { key1:value1, key2:value2} string.
        /// Type string and datetime are doublequoted for the keys and values.
        /// </summary>
        /// <typeparam name="K"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="dict"></param>
        /// <returns></returns>
        public static string DictionaryToString <K, V>(IDictionary <K, V> dict)
        {
            if (dict == null)
            {
                return("Null");
            }                                    // just in case

            System.Text.StringBuilder b = new StringBuilder(1024);
            b.Append("{");

            foreach (KeyValuePair <K, V> kv in dict)
            {
                string value = ExtendedFormat.FormatValue(kv.Value);
                string key   = ExtendedFormat.FormatValue(kv.Key);
                b.AppendFormat("{0}:{1}, ", key, value);
            }
            b.Remove(b.Length - System.Environment.NewLine.Length, System.Environment.NewLine.Length); // Remove the coma and the space
            b.Append("}");
            return(b.ToString());
        }
        //public static string Format(this string s, object a1) { return string.Format(s, a1); }
        //public static string Format(this string s, object a1, object a2) { return string.Format(s, a1, a2); }
        //public static string Format(this string s, object a1, object a2, object a3) { return string.Format(s, a1, a2, a3); }
        //public static string Format(this string s, object a1, object a2, object a3, object a4) { return string.Format(s, a1, a2, a3, a4); }
        //public static string Format(this string s, object a1, object a2, object a3, object a4, object a5) { return string.Format(s, a1, a2, a3, a4, a5); }
        //public static string Format(this string s, object a1, object a2, object a3, object a4, object a5, object a6) { return string.Format(s, a1, a2, a3, a4, a5, a6); }
        //public static string Format(this string s, object a1, object a2, object a3, object a4, object a5, object a6, object a7) { return string.Format(s, a1, a2, a3, a4, a5, a6, a7); }
        //public static string Format(this string s, object a1, object a2, object a3, object a4, object a5, object a6, object a7, object a8) { return string.Format(s, a1, a2, a3, a4, a5, a6, a7, a8); }
        //public static string Format(this string s, object a1, object a2, object a3, object a4, object a5, object a6, object a7, object a8, object a9) { return string.Format(s, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
        //public static string Format(this string s, object a1, object a2, object a3, object a4, object a5, object a6, object a7, object a8, object a9, object a10) { return string.Format(s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); }

        /*
         * /// <summary>
         * /// http://extensionmethod.net/csharp/string/FormatString-string
         * /// Author: Adam Weigert
         * /// </summary>
         * /// <param name="FormatString"></param>
         * /// <param name="arg"></param>
         * /// <param name="additionalArgs"></param>
         * /// <returns></returns>
         * public static string Formataaaaa(this string FormatString, object arg, params object[] additionalArgs)
         * {
         *  if (additionalArgs == null || additionalArgs.Length == 0)
         *  {
         *      return string.Format(FormatString, arg);
         *  }
         *  else
         *  {
         *      return string.Format(FormatString, new object[] { arg }.Concat(additionalArgs).ToArray());
         *  }
         * }*/
        /// <summary>
        ///  Replaces the FormatString item in the string with the string representation
        ///  of a corresponding property/field in the object passed.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="poco">Any poco object</param>
        /// <returns></returns>
        public static string Template(this string s, object poco)
        {
            return(ExtendedFormat.Format(s, ReflectionHelper.GetDictionary(poco)));
        }