重构此代码的最佳方法是什么?有没有更好的方法来重新编码此片段?
if (doc.fldUpphandlingFilerStatus != null)
{
if (DocumentFileStatus == Enums.FileStatus.UnpublishedNotice ||
DocumentFileStatus == Enums.FileStatus.UnpublishedOriginalNotice ||
DocumentFileStatus == Enums.FileStatus.UnpublishedNewNoticeWithOpicData ||
DocumentFileStatus == Enums.FileStatus.UnpublishedNewNotice)
{
icon = IconType.UnpublishedNotice;
}
if (DocumentFileStatus == Enums.FileStatus.PublishedNotice ||
DocumentFileStatus == Enums.FileStatus.PublishedOriginalNotice)
{
icon = IconType.PublishedNotice;
}
if (DocumentFileStatus == Enums.FileStatus.UnpublishedContractAward ||
DocumentFileStatus == Enums.FileStatus.UnpublishedFile ||
DocumentFileStatus == Enums.FileStatus.UnpublishedFileOtherReason)
{
icon = IconType.UnpublishedDocument;
}
if (DocumentFileStatus == Enums.FileStatus.PublishedAgreement ||
DocumentFileStatus == Enums.FileStatus.PublishedContractAward ||
DocumentFileStatus == Enums.FileStatus.PublishedCourtCase ||
DocumentFileStatus == Enums.FileStatus.PublishedFile ||
DocumentFileStatus == Enums.FileStatus.PublishedTender)
{
icon = IconType.PublishedDocument;
}
if (DocumentFileStatus == Enums.FileStatus.PublishedFileAfterTimeLimit)
{
icon = IconType.UnpublishedTenderingPeriod;
}
if (DocumentFileStatus == Enums.FileStatus.UnpublishedFileOtherReason)
{
icon = IconType.AlwaysUnpublished;
}
if (DocumentFileStatus == Enums.FileStatus.EmptyFile ||
DocumentFileStatus == Enums.FileStatus.FileNotFound)
{
icon = IconType.BrokenDocument;
}
if (DocumentFileStatus == Enums.FileStatus.UnpublishedLink)
{
icon = IconType.UnpublishedLink;
}
if (DocumentFileStatus == Enums.FileStatus.PublishedLink)
{
icon = IconType.PublishedLink;
}
}
答案 0 :(得分:3)
切换语句?
switch (DocumentFileStatus)
{
case Enums.FileStatus.UnpublishedNotice:
case Enums.FileStatus.UnpublishedOriginalNotice:
case Enums.FileStatus.UnpublishedNewNoticeWithOpicData:
case Enums.FileStatus.UnpublishedNewNotice:
icon = IconType.UnpublishedNotice;
break;
case Enums.FileStatus.PublishedNotice:
case Enums.FileStatus.PublishedOriginalNotice:
icon = IconType.PublishedNotice;
break;
}
答案 1 :(得分:2)
您可以创建映射字典:
Dictionary<FileStatus, IconType> mapping = new Dictionary<FileStatus, IconType>
{
{Enums.FileStatus.UnpublishedNotice, IconType.UnpublishedNotice}
...
}
然后获取适当的值为:
IconType icon;
if(!mapping.TryGetValue(DocumentFileStatus, out icon))
{
icon = someDefaultValue;
}
答案 2 :(得分:0)
我会用字典
Dictionary<Enums.FileStatus, IconType> dict = new Dictionary<Enums.FileStatus, IconType>()
{
{ Enums.FileStatus.UnpublishedNotice , IconType.UnpublishedNotice },
{ Enums.FileStatus.UnpublishedOriginalNotice , IconType.UnpublishedNotice },
{ Enums.FileStatus.UnpublishedNewNoticeWithOpicData , IconType.UnpublishedNotice },
{ Enums.FileStatus.UnpublishedNewNotice, IconType.UnpublishedNotice },
{ Enums.FileStatus.PublishedNotice ,IconType.PublishedNotice },
{ Enums.FileStatus.PublishedOriginalNotice,IconType.PublishedNotice },
{ Enums.FileStatus.UnpublishedContractAward ,IconType.UnpublishedDocument },
{ Enums.FileStatus.UnpublishedFile ,IconType.UnpublishedDocument },
{ Enums.FileStatus.UnpublishedFileOtherReason, IconType.UnpublishedDocument },
{ Enums.FileStatus.PublishedAgreement ,IconType.PublishedDocument },
{ Enums.FileStatus.PublishedContractAward ,IconType.PublishedDocument },
{ Enums.FileStatus.PublishedCourtCase ,IconType.PublishedDocument },
{ Enums.FileStatus.PublishedFile ,IconType.PublishedDocument },
{ Enums.FileStatus.PublishedTender,IconType.PublishedDocument },
{ Enums.FileStatus.PublishedFileAfterTimeLimit,IconType.UnpublishedTenderingPeriod },
{ Enums.FileStatus.UnpublishedFileOtherReason, IconType.AlwaysUnpublished },
{ Enums.FileStatus.EmptyFile , IconType.BrokenDocument },
{ Enums.FileStatus.FileNotFound, IconType.BrokenDocument },
{ Enums.FileStatus.UnpublishedLink,IconType.UnpublishedLink },
{ Enums.FileStatus.PublishedLink,IconType.PublishedLink }
};
icon = dict[DocumentFileStatus];