小编Stu*_*lah的帖子

重载最佳实践

我有两个静态方法,我想用于错误处理.其中一个传递异常对象,另一个只是在需要报告错误时使用,该错误是基于文本的消息(字符串errorMessage).

除了如何构建消息并将其发送到日志文件之外,两种方法中的代码几乎相同.我怎样才能重构这个以便我不重复代码?

public static void ReportError(Exception exceptionRaised, string reference, string customMessage, bool sendEmail)
{
    // get filename
    // check if logfile exists, blah, blah
    // build up message from exception, reference & custom message using string builder
    // save message
    // email error (if set)
}

public static void ReportError(string errorMessage, string reference, bool sendEmail)
{
    // get filename
    // check if logfile exists, blah, blah
    // build up message from errorMessage & reference string builder
    // save message
    // …
Run Code Online (Sandbox Code Playgroud)

c# overloading

4
推荐指数
2
解决办法
1089
查看次数

标签 统计

c# ×1

overloading ×1