方法中大量if判斷,利用列舉進行優化

2021-09-26 22:50:54 字數 1431 閱讀 9708

在乙個方法類中有大量if語句判斷,有沒有大佬幫忙看看,怎麼用列舉的方式進行優化

public locatedatasourceeventhandler sourcelist(string requestid,string reportname)

if (reportname.contains(「wb_zty」) || reportname.contains(「wb_fty」)) // 貨物託運單

if (reportname.contains(「wb_dt」) || reportname.contains(「wb_bdt」)) // 嘜頭

if (reportname.contains(「wb_bh_add」)) //附頁清單

if (reportname.contains(「wb_hal」)) //艙單

if (reportname.contains(「wb_bh」)) //保函

if (reportname.contains(「wb_wts」)) //保函

if (reportname.contains(「wb_ntwts」)) //保函

if (reportname.contains(「ghs_dg」)) // ghs

if (reportname.contains(「ghs_sh」)) // ghs

if (reportname.contains(「ghs_mt」)) // ghs

if (reportname.contains(「tcp_hwsq」)) // 通關單

if (reportname.contains(「tcp_fj」)) // 通關單

}以上是之前未解決的大量if判斷,想著找到了方法解決

首先建立兩個我們當前需要判斷的兩個列舉,乙個是我們類名的列舉,乙個是所有類名中方法的列舉

當然在我們建立類名,和建立類中方法的是否,兩者之間的名稱是有一定關聯的

一下是我專案中所定義的方法規則,大家可以借鑑一下

report類名取名方法為——***report

*** 取名必須和報表名字首相對應——報表名:*@@@.ralx

***report中方法取名為——***@@@datasource

report中方法名需和報表名相互對應

//getreportsourscesenum 此名稱為我所有類中的方法集合的列舉

//getreportname 此名稱為我所有需要判斷的類名的集合

type reportsources = typeof(getreportsourscesenum); //建立 列舉enum type

fieldinfo sourcesfieldinfo = reportsources.getfields(); //獲取列舉enum type中的引數名

for (int u = 1; u < sourcesfieldinfo.length; u++)}}

}

利用佇列, 進行大量檔案批處理

依然以關於批量轉換成webp格式為例,所有的都在本地的upload資料夾 原理 採用佇列而不是遞迴來批量處理檔案,對記憶體的占用要小很多,處理了28429個,不會提示記憶體超出 主要看開頭和結尾的陣列和目錄的關係 set time limit 0 0表示不限時 ini set gd.jpeg ign...

利用列舉進行狀態的設計

在上一家公司的時候,髮像乙個很不好的地方,那就是乙個實體的狀態使用int來表示。個人覺的主要有以下一些缺點。列舉的優點 1 雖然int是c 中的強型別,但int不是業務邏輯的強型別,但列舉可以實現業務邏輯的強型別。比如說乙個訂單的狀態如果使用列舉的話,那麼就能確定它能安全的表示這些狀態,而如果直接使...

列舉轉化和判斷方法

定義列舉 public enum displaytype 1.數值轉化 1 字元轉化為列舉 string str up displaytype displaytype displaytype displaytype system.enum.parse typeof displaytype str,t...