使用以下代碼,如何使用反射和泛型提高其可讀性?這是很多重復的代碼,但我對這些概念很陌生。“dl”是一個介面,但我不確定為什么我無法使用 .getType().getProperties() 訪問我想要的屬性,它回傳一個空串列。
if (entity == "SERVICE")
{
if (filter == "Active == 1")
{
foreach (var service in dl.Services.List().Where(x => x.Active).OrderBy(x => x.Name))
tmpReturn.Add(service.ID, service.Name);
return tmpReturn;
}
foreach (var service in dl.Services.List().OrderBy(x => x.Name))
tmpReturn.Add(service.ID, service.Name);
return tmpReturn;
}
if (entity == "CAMPAIGN")
{
if (filter == "Active == 1")
{
foreach (var campaign in dl.Campaigns.List().Where(x => x.Active).OrderBy(x => x.Name))
tmpReturn.Add(campaign.ID, campaign.Name);
return tmpReturn;
}
foreach (var campaign in dl.Campaigns.List().OrderBy(x => x.Name))
tmpReturn.Add(campaign.ID, campaign.Name);
return tmpReturn;
}
if (entity == "AGENT")
{
if (condition == "Active != ")
{
if (value == "1")
{
foreach (var agent in dl.Agents.List().OrderBy(x => x.Name))
tmpReturn.Add(agent.ID, agent.Name);
return tmpReturn;
}
foreach (var agent in dl.Agents.List().Where(x => x.Active).OrderBy(x => x.Name))
tmpReturn.Add(agent.ID, agent.Name);
return tmpReturn;
}
else if (condition == "Active == ")
{
if (value == "1")
{
foreach (var agent in dl.Agents.List().Where(x => x.Active).OrderBy(x => x.Name))
tmpReturn.Add(agent.ID, agent.Name);
return tmpReturn;
}
foreach (var agent in dl.Agents.List().OrderBy(x => x.Name))
tmpReturn.Add(agent.ID, agent.Name);
return tmpReturn;
}
}
uj5u.com熱心網友回復:
由于不同的物體沒有實作共同的介面,我們使用ValueTuples作為中間結果。我不確定 ID 型別是什么。我在int這里假設,但你可以很容易地改變它。
IEnumerable<(int ID, string Name, bool Active)> source;
bool filterByActive;
switch (entity) {
case "SERVICE":
source = dl.Services.List().Select(x => (x.ID, x.Name, x.Active));
filterByActive = filter == "Active == 1";
break;
case "CAMPAIGN":
source = dl.Campaigns.List().Select(x => (x.ID, x.Name, x.Active));
filterByActive = filter == "Active == 1";
break;
case "AGENT":
source = dl.Agents.List().Select(x => (x.ID, x.Name, x.Active));
filterByActive = condition == "Active != " && value != "1" ||
condition == "Active == " && value == "1";
break;
default:
throw new NotImplementedException();
}
if (filterByActive) {
source = source.Where(x => x.Active);
}
return source
// .OrderBy(x => x.Name) Not for Dictionary!
.ToDictionary(x => x.ID, x => x.Name);
這個想法是我們回傳一個表示資料源的列舉和一個布林值,告訴我們是否必須在 switch 陳述句中進行過濾。然后可以在此資料源上執行所有其他操作。
Where我們可以通過單獨應用來組成可列舉。
使用 LINQ 擴展方法ToDictionary()而不是在回圈中添加單個元素可以進一步簡化事情。請注意,如果您將內容添加到字典中,則對專案進行排序無效。
uj5u.com熱心網友回復:
我認為您不需要反射或泛型:(我沒有在以下代碼片段中撰寫型別,因為我不能總是從您的代碼中推斷出它們。)
List getTmpReturn(list, bool isActive){
if (isActive){
foreach (var item in list.List().Where(x => x.Active).OrderBy(x => x.Name))
tmpReturn.Add(item.ID, item.Name);
}
else {
foreach (var item in list.List().OrderBy(x => x.Name))
tmpReturn.Add(item.ID, item.Name);
}
return tmpReturn;
}
switch(entity):
case "SERVICE":
return getTmpReturn(dl.Services, filter == "Active == 1");
case "CAMPAIGN":
return getTmpReturn(dl.Campaigns, filter == "Active == 1");
case "AGENT":
bool isActive = (condition == "Active != " && value != "1") || (condition == "Active == " && value == "1");
return getTmpReturn(dl.Agents, isActive);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/426687.html
上一篇:如何在Kotlin中使用泛型函式作為引數創建泛型函式?
下一篇:如何使函式回傳結構的泛型?
