What's your most reused class?

前端 未结 2 648
盖世英雄少女心
盖世英雄少女心 2021-02-12 19:11

Every programmer ends up with a set of utility classes after a while. Some of them are true programming pearls and they are reused in several of your projects. For example, in j

相关标签:
2条回答
  • 2021-02-12 19:25

    System.Object - almost all my types extend it.

    0 讨论(0)
  • 2021-02-12 19:46

    A utility class that has logging and email functionality. An extensions class that contains extension methods. A reporting class that basically harness the reporting services web service and makes it easy to stream reports as excel, pdf, etc.

    Examples...
    1.) Utility Class (static)

       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.) Extensions Class

       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;
        }
    
    0 讨论(0)
提交回复
热议问题