发送HTML参数和文件路径参数?

soo*_*ise 4 c# methods arguments class-design

我正在创建一个需要打印HTML字符串和HTML文档的打印机类.所以基本上它可以得到:

Printer.Print("<b>Hello world</b>");
Run Code Online (Sandbox Code Playgroud)

Printer.Print(@"C:\hello.html");
Run Code Online (Sandbox Code Playgroud)

所以在设计我的类时,Print方法定义我决定在以下方面:

public static void Print(string inputString, string mode){
    if(mode=="htmlString"){//Print the string itself}
    else if(mode=="htmlFile"){//Print the document in the filepath}
}
Run Code Online (Sandbox Code Playgroud)

要么

public static void Print(string inputString){
    if(file.Exists(inputString)){//Print the document in the filepath}
    else{//Print the string itself}
}
Run Code Online (Sandbox Code Playgroud)

一般来说,哪种做法更好?第一个选项需要另一个不太好的参数,但是如果我们使用第二个选项,如果我们打算实际打印文件但使用不正确的文件名,它将打印错误的东西.

Gra*_*mas 5

很多时候,偶然事件的空间太大,特别是在这种情况下你必须根据输入确定如何行动,然后进一步做验证处理(即File.Exists),它正在为误报而哭泣.在我看来,做这样的事情:

public static void PrintString(string input)
{
    //print the string, knowing precisely this is the intent,
    //and if not, it's what you're going to do anyway!
}

public static void PrintFile(string fileName)
{
    //no qualms here, you're going to print a file
}
Run Code Online (Sandbox Code Playgroud)