我需要帮助改进我的字符串格式化程序

Bra*_*rad 3 c#

我试图根据类型格式化字符串...但我觉得它有点难看.谁有任何sugestions更精致的approch?

 string DateFormat = "MMM dd yyyy";
 string NumberFormat = "0.0";

string FormatString(Type t, object value)
    {
        string output = "";

        switch (t.Name.ToUpper())
        {

            case "DATETIME":
                output = ((DateTime)value).ToString(DateFormat);
                break;
            case "SINGLE":
                output = ((Single)value).ToString(NumberFormat);
                break;
            case "DOUBLE":
                output = ((Double)value).ToString(NumberFormat);
                break;
            default:
                output = value.ToString();
                break;
        }

        return output;

    }
Run Code Online (Sandbox Code Playgroud)

Pao*_*sco 7

为什么不重载方法呢?

using System;

namespace test {

    static class Formatter {

        const string DateFormat = "MMM dd yyyy";
        const string NumberFormat = "0.0";

        public static string Format(double d) {
            return d.ToString(NumberFormat);
        }

        public static string Format(DateTime d) {
            return d.ToString(DateFormat);
        }

        // most generic method
        public static string Format(object o) {
            return o.ToString();
        }

    }

    class Program {

        public static void Main() {
            Console.WriteLine(Formatter.Format(2.0d));
            Console.WriteLine(Formatter.Format(DateTime.Now));
            // an integer => no specific function defined => pick the
            // most generic overload (object)
            Console.WriteLine(Formatter.Format(4));
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

注意:如果需要比较类型,则应使用

if (typeof(int) == t){
    // ...
}
Run Code Online (Sandbox Code Playgroud)

并且不对类型名称进行比较(或者至少,如果这样做,请检查完全限定的类型名称,即包括命名空间).

编辑
一个替代解决方案,它考虑到Allon的评论,使用字典类型 - >函数:

using System;
using System.Collections.Generic;

namespace test {

    public class Formatter {

        delegate string FormatFunction(object o);

        private string FormatDouble(object o) {
            double d = (double)o;
            return d.ToString("0.0");
        }

        private string FormatDateTime(object o) {
            DateTime d = (DateTime)o;
            return d.ToString("MMM dd yyyy");
        }

        // map types to format function
        private Dictionary<Type, FormatFunction> _formatters = new Dictionary<Type, FormatFunction>();

        public Formatter() {
            _formatters.Add(typeof(double), FormatDouble);
            _formatters.Add(typeof(DateTime), FormatDateTime);
        }

        public string Format(object o) { 
            Type t = o.GetType();
            if (_formatters.ContainsKey(t)){
                return _formatters[t](o);
            } else {
                return o.ToString();
            }
        }

    }

    class Program {
        public static void Main() {
            Formatter f = new Formatter();
            Console.WriteLine(f.Format(2.0d));
            Console.WriteLine(f.Format(DateTime.Now));
            Console.WriteLine(f.Format(4));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用第二个解决方案,即使您只是引用了一个对象,也可以获得正确的功能(如果可能,我仍会使用第一个解决方案).