2013-02-28 73 views
1

我有這段代碼,我覺得可以清理(我可能是錯的),但我想看看是否有人有一個建議,我可以改變這是「更好」重構if-else語句檢查不同的文件擴展名

string getExt = Path.GetExtension(DocumentUNCPath.Text); 
     var convertFileId = Guid.NewGuid(); 
     var convertFilePath = @"c:\temp\" + convertFileId + ".pdf"; 


     if (getExt == ".doc" || getExt == ".docx" || getExt == ".txt" || getExt == ".rtf") 
     { 
      WordToPdf(convertFilePath); 

     } 
     else if (getExt == ".xlsx" || getExt == ".xls") 
     { 
      ExcelToPdf(convertFilePath); 
     } 
     else if (getExt == ".jpg" || getExt == ".png" || getExt == ".jpeg" || getExt == ".JPG" || getExt == ".PNG") 
     { 
      ImgToPDF(convertFilePath); 
     } 
+0

我覺得使用它已經足夠了。 – Stepo 2013-02-28 19:49:30

+0

至少,執行'getExt = getExt.ToLower()'並從條件中移除冗餘etensions。 – 2013-02-28 20:05:31

回答

1

以下可能最初是更多的代碼,但可以更好地擴展。

之外的方法:

public static readonly List<string> WorkExtensions = new List<string> { ".doc", ".docx", ".txt", ".trf" }; 
    public static readonly List<string> ExcelExtensions = new List<string> { ".xlsx", ".xls" }; 
    public static readonly List<string> ImageExtensions = new List<string> { ".jpg", ".png", ".jpeg" }; 

裏面的方法:

string getExt = Path.GetExtension(DocumentUNCPath.Text); 
    var convertFileId = Guid.NewGuid(); 
    var convertFilePath = @"c:\temp\" + convertFileId + ".pdf"; 

    getExt = getExt.ToLower(); 

    if (WorkExtensions.Contains(getExt)) 
    { 
     WordToPdf(convertFilePath) 
    } 
    else if (ExcelExtensions.Contains(getExt)) 
    { 
     ExcelToPdf(convertFilePath); 
    } 
    else if (ImageExtensions.Contains(getExt)) 
    { 
     ImgToPdf(convertFilePath); 
    } 
15

地圖擴展處理的是這樣的情況下,標準的方法:

// populate with { ".doc", WordToPdf } and similar pairs 
Dictionary<string, Action<string> > handlers = ... 


// find and call handler by extension 
// (use TryGetValue to check for existence if needed) 
handlers[getExt](convertFilePath); 
+2

我想補充一點,因爲文件名不區分大小寫,所以擴展名不區分大小寫。即'新字典<字符串,動作>(StringComparer.OrdinalIgnoreCase)' – 2013-02-28 20:01:22

+0

對於密鑰值而言,列表「」而不是「字符串」會更有意義嗎?這是爲了防止您希望多個分機進行相同的操作。 – gunr2171 2013-02-28 20:10:37

+0

@ gunr2171不,使用'List '作爲關鍵是沒有意義的,它實際上會破壞使用'Dictionary'的目的。在使用'List '時,您會使查找速度變慢,因爲您需要檢查每個密鑰,直到找到包含所需擴展名的密鑰,然後使用該密鑰返回該值。 – 2013-02-28 20:31:40

4

你可以做這樣的事情

switch (getExt.ToUpper()) 
    {  
     case "JPG":  
     case "PNG": 
.... 
1

如果您正在尋找extensiblity,你可以conisder是這樣的:

public struct Converter { 
    public string   Extension; 
    public Action<string> ConvertAction; 
} 

public static class Extensions { 
    static Action<string> WordToPdf = (s) => {;}; 
    static Action<string> ExcelToPdf = (s) => {;}; 
    static Action<string> ImgToPdf = (s) => {;}; 

    public static IEnumerable<Converter> Converters = new List<Converter> { 
    new Converter {Extension = ".doc", ConvertAction = WordToPdf}, 
    new Converter {Extension = ".docx", ConvertAction = WordToPdf}, 
    new Converter {Extension = ".txt", ConvertAction = WordToPdf}, 
    new Converter {Extension = ".rtf", ConvertAction = WordToPdf}, 

    new Converter {Extension = ".xls", ConvertAction = ExcelToPdf}, 
    new Converter {Extension = ".xlsx", ConvertAction = ExcelToPdf}, 

    new Converter {Extension = ".jpg", ConvertAction = ImgToPdf}, 
    new Converter {Extension = ".png", ConvertAction = ImgToPdf}, 
    new Converter {Extension = ".jpeg", ConvertAction = ImgToPdf}, 
    new Converter {Extension = ".doc", ConvertAction = ImgToPdf} 
    }; 

    public void RunIt(string extension, string convertFilePath) { 
    extension = extension.ToLower(); 
    var action = (from a in Converters 
        where a.Extension.Equals(extension) 
        select a.ConvertAction).First(); 
    if (action != null) action(convertFilePath); 
    } 
} 
+1

今天我學到了新東西。哇!這看起來非常整齊,即使它比我開始的代碼更多,但我也會嘗試這一點。謝謝! – BB987 2013-02-28 20:13:16

+0

這是更多的代碼,但增加了一些nore擴展,並達到盈虧平衡點。另外,由於數據驅動,它可以在運行時從外部文本文件加載。 – 2013-02-28 21:27:14

2

我覺得Dictionary<string, Action<string>>以上答案是最優雅的答案,但爲了完整起見,這裏有一個解決方案通過串擴展:

public static class StringExt 
{ 
    public static bool MatchesAnyOf(this string text, params string[] targets) 
    { 
     return targets.Any(target => string.Compare(text, target, StringComparison.OrdinalIgnoreCase) == 0); 
    } 
} 

然後,你可以寫這樣的代碼:

if (getExt.MatchesAnyOf(".doc", ".doxc", ".txt", ".rtf")) 
{ 
    WordToPdf(convertFilePath); 
} 
else if (getExt.MatchesAnyOf(".xlsx", ".xls")) 
{ 
    ExcelToPdf(convertFilePath); 
} 
else if (getExt.MatchesAnyOf(".jpg", ".png", ".jpeg", ".JPG", ".PNG") 
{ 
    ImgToPDF(convertFilePath); 
} 

此實現忽略大小寫和文化,這是適當的文件名,但將不適合用於一般用途 - 所以真正的代碼可能會提供指定文化和比較類型的重載。