假設我有2周類似的方法命名的GetFiles()和PutFiles()瘦身多種類似方法的合適方法是什麼?
public static void GetFiles(string source, string dest)
{
using (Session session = new Session())
{
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
////Different in here////
transferResult = session.GetFiles(source, dest, false, transferOptions);
transferResult.Check();
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Transfer file from {0} to {1}", source, dest);
}
}
}
public static void PutFiles(string source, string dest)
{
using (Session session = new Session())
{
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
////Different in here////
transferResult = session.PutFiles(source, dest, false, transferOptions);
transferResult.Check();
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Transfer file from {0} to {1}", source, dest);
}
}
}
你可以看到他們是除了方法 我有考慮添加一個名爲傳輸文件的新方法中調用不同的功能完全一樣,其中接受一個參數來控制放文件的過程/獲取文件
public static void TransferFile(string source, string dest, bool GetFile)
{
using (Session session = new Session())
{
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
if(GetFile)
transferResult = session.GetFiles(source, dest, false, transferOptions);
else
transferResult = session.PutFiles(source, dest, false, transferOptions);
transferResult.Check();
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Transfer file from {0} to {1}", source, dest);
}
}
}
但後來我發現,這不是一個最佳的解決方案,因爲我不能處理超過2條件(獲取文件&放文件)
你是什麼意思,它不能處理超過2個條件? – 2014-09-11 03:22:06
如果我還有一個函數爲 - transferResult = session.CompareFiles(source,dest,false,transferOptions); ,那麼我將不得不將TransferFile(string source,string dest,bool GetFile)的參數更改爲TransferFile(string source,string dest,int FileAction),這樣我可以處理更多不同的過程 – 2014-09-11 03:23:37
但我不認爲這個解決方案是很好所有 – 2014-09-11 03:26:34