1

私はこのコードをクリーンアップできると感じています(私は間違っている可能性があります)が、これを「より良い」ものに変更する方法を誰かが提案しているかどうかを見たかったのです

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);
        }
4

5 に答える 5

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 );
于 2013-02-28T19:51:21.403 に答える
4

このようなことができます

    switch (getExt.ToUpper()) 
    {    
       case "JPG":    
       case "PNG": 
....
于 2013-02-28T19:51:24.557 に答える
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);
}

この実装は、ファイル名には適切ですが、一般的な使用には適切ではない大文字小文字とカルチャを無視します。したがって、実際のコードは、カルチャと比較タイプを指定するためのオーバーロードを提供する必要があります。

于 2013-02-28T20:38:16.050 に答える
1

拡張性を探している場合は、次のようなものを検討できます。

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);
  }
}
于 2013-02-28T20:04:10.817 に答える
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);
    }
于 2013-02-28T19:58:39.533 に答える