C# 重构if-else语句检查不同的文件扩展名
我有这段代码,我觉得可以清理(我可能是错的),但我想看看是否有人对我如何将其更改为“更好”有什么建议C# 重构if-else语句检查不同的文件扩展名,c#,asp.net,C#,Asp.net,我有这段代码,我觉得可以清理(我可能是错的),但我想看看是否有人对我如何将其更改为“更好”有什么建议 扩展到处理程序的映射是此类情况的标准方法: // populate with { ".doc", WordToPdf } and similar pairs Dictionary<string, Action<string> > handlers = ... // find and call handler by extension // (use TryGetV
扩展到处理程序的映射是此类情况的标准方法:
// 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 );
//用{.doc',WordToPdf}和类似的对填充
字典处理程序=。。。
//按扩展名查找并调用处理程序
//(如果需要,使用TryGetValue检查是否存在)
处理程序[getExt](convertFilePath);
扩展到处理程序的映射是此类情况的标准方法:
// 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 );
//用{.doc',WordToPdf}和类似的对填充
字典处理程序=。。。
//按扩展名查找并调用处理程序
//(如果需要,使用TryGetValue检查是否存在)
处理程序[getExt](convertFilePath);
您可以这样做
switch (getExt.ToUpper())
{
case "JPG":
case "PNG":
....
你可以这样做
switch (getExt.ToUpper())
{
case "JPG":
case "PNG":
....
以下内容最初可能是更多的代码,但可以更好地扩展 方法之外:
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);
}
以下内容最初可能是更多的代码,但可以更好地扩展 方法之外:
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);
}
如果您正在寻找扩展性,您可以这样做:
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);
}
}
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);
}
公共结构转换器{
公共字符串扩展;
公共行动;
}
公共静态类扩展{
静态动作WordToPdf=(s)=>{;};
静态操作ExcelToPdf=(s)=>{;};
静态作用ImgToPdf=(s)=>{;};
公共静态IEnumerable转换器=新列表{
新转换器{Extension=“.doc”,ConvertAction=WordToPdf},
新转换器{Extension=“.docx”,ConvertAction=WordToPdf},
新转换器{Extension=“.txt”,ConvertAction=WordToPdf},
新转换器{Extension=“.rtf”,ConvertAction=WordToPdf},
新转换器{Extension=“.xls”,ConvertAction=ExcelToPdf},
新转换器{Extension=“.xlsx”,ConvertAction=ExcelToPdf},
新转换器{Extension=“.jpg”,ConvertAction=ImgToPdf},
新转换器{Extension=“.png”,ConvertAction=ImgToPdf},
新转换器{Extension=“.jpeg”,ConvertAction=ImgToPdf},
新转换器{Extension=“.doc”,ConvertAction=ImgToPdf}
};
public void RunIt(字符串扩展名,字符串转换文件路径){
extension=extension.ToLower();
var动作=(来自转换器中的a)
其中a.Extension.Equals(Extension)
选择a.ConvertAction).First();
if(action!=null)action(convertFilePath);
}
}
如果您正在寻找可扩展性,您可以这样做:
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);
}
}
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);
}
公共结构转换器{
公共字符串扩展;
公共行动;
}
公共静态类扩展{
静态动作WordToPdf=(s)=>{;};
静态操作ExcelToPdf=(s)=>{;};
静态作用ImgToPdf=(s)=>{;};
公共静态IEnumerable转换器=新列表{
新转换器{Extension=“.doc”,ConvertAction=WordToPdf},
新转换器{Extension=“.docx”,ConvertAction=WordToPdf},
新转换器{Extension=“.txt”,ConvertAction=WordToPdf},
新转换器{Extension=“.rtf”,ConvertAction=WordToPdf},
新转换器{Extension=“.xls”,ConvertAction=ExcelToPdf},
新转换器{Extension=“.xlsx”,ConvertAction=ExcelToPdf},
新转换器{Extension=“.jpg”,ConvertAction=ImgToPdf},
新转换器{Extension=“.png”,ConvertAction=ImgToPdf},
新转换器{Extension=“.jpeg”,ConvertAction=ImgToPdf},
新转换器{Extension=“.doc”,ConvertAction=ImgToPdf}
};
public void RunIt(字符串扩展名,字符串转换文件路径){
extension=extension.ToLower();
var动作=(来自转换器中的a)
其中a.Extension.Equals(Extension)
选择a.ConvertAction).First();
if(action!=null)action(convertFilePath);
}
}
我认为上面的字典
答案是最优雅的答案,但为了完整起见,这里有一个通过字符串扩展的解决方案:
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);
}
}
然后您可以这样编写代码:
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);
}
}
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);
}
此实现忽略了大小写和区域性,它们适用于文件名,但不适用于一般用途-因此真正的代码可能必须提供重载来指定区域性和比较类型。我认为上面的
字典答案是最优雅的答案,但只是为了完整性,下面是一个通过字符串扩展名实现的解决方案:
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);
}
}
然后您可以这样编写代码:
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);
}
}
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);
}
这个实现忽略了大小写和区域性,它们适用于文件名,但不适用于一般用途-因此真正的代码可能必须提供重载来指定区域性和比较类型。我认为使用它就足够了。至少,请执行getExt=getExt.ToLower()
并从条件中删除te冗余的etensions。我认为使用它就足够了。至少,执行getExt=getExt.ToLower()
并从条件中删除te冗余的etensions。我要补充一点,因为文件名不区分大小写,所以扩展名的比较也不区分大小写。i、 e.新字典(StringComparer.OrdinalIgnoreCase)
作为键值的列表而不是字符串是否更有意义?这是在您希望多个扩展指向同一操作的情况下发生的。@gunr2171否使用List
作为键是没有意义的,它实际上会破坏使用字典的目的。在使用列表时,您会使查找速度变慢,因为您需要检查每个键,直到找到包含所需扩展名的键,然后在该键处返回该值。我要补充一点,因为文件名不区分大小写,所以扩展名的比较也不区分大小写。i、 e.新词典(StringComparer.OrdinalIgnoreCase)
作为列表而不是列表更有意义吗