不久之后,每个程序员都会得到一组实用程序类。其中有些是真正的编程明珠,并且可以在您的几个项目中重复使用。例如,在java中:

 class Separator {

        private String separator;
        private boolean called;

        public Separator(String aSeparator) {
            separator = aSeparator;
            called = false;
        }

        @Override
        public String toString() {
            if (!called) {
                called = true;
                return "";
            } else {
                return separator;
            }
        }
    }


和:

public class JoinHelper {

    public static <T> String join(T... elements) {
        return joinArray(" ", elements);
    }

    public static <T> String join(String separator, T... elements) {
        return joinArray(separator, elements);
    }

    private static <T> String joinArray(String sep, T[] elements) {
        StringBuilder stringBuilder = new StringBuilder();
        Separator separator = new Separator(sep);

        for (T element : elements) {
           stringBuilder.append(separator).append(element);
        }

        return stringBuilder.toString();
    }
}


您最经常重复使用的课程是什么?

最佳答案

具有日志和电子邮件功能的实用程序类。包含扩展方法的扩展类。一个报告类,基本上利用了报告服务Web服务,并可以轻松地以excel,pdf等格式流式传输报告。

例子...
1.)实用程序类(静态)

   public static void LogError(Exception ex)
    {
        EventLog log = new EventLog();
        if (ex != null)
        {
            log.Source = ConfigurationManager.AppSettings["EventLog"].ToString();
            StringBuilder sErrorMessage = new StringBuilder();
            if (HttpContext.Current.Request != null && HttpContext.Current.Request.Url != null)
            {
                sErrorMessage.Append(HttpContext.Current.Request.Url.ToString() + System.Environment.NewLine);
            }
            sErrorMessage.Append(ex.ToString());
            log.WriteEntry(sErrorMessage.ToString(), EventLogEntryType.Error);
        }
    }


2.)扩展类

   public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, bool> predicate)
    {
        if (condition)
            return source.Where(predicate);
        else
            return source;
    }

07-24 20:16