在c#中,運算式能否、如何作為引數傳遞?
比如下面的方法中,if的運算式實際可能非常復雜、多變,需要根據鍵值的內容變化。此時應該如何處理?謝謝
private void Test()
{
dataGridView.Rows.Clear();
foreach (var name in dic.Keys)
{
if (name.StartsWith("Final") || name.StartsWith("Sub")) //此處應如何設計滿足不同的需求?
{
int rowIndex = dataGridView.Rows.Add();
foreach (DataGridViewColumn column in dataGridView.Columns)
{
DataGridViewCell currentCell = dataGridView.Rows[rowIndex].Cells[column.Index];
currentCell.Value = dic[name];
}
}
}
}
uj5u.com熱心網友回復:
test(func<string,bool> func){
dataGridView.Rows.Clear();
foreach (var name in dic.Keys)
{
if (func(name)) //此處應如何設計滿足不同的需求?
{
int rowIndex = dataGridView.Rows.Add();
foreach (DataGridViewColumn column in dataGridView.Columns)
{
DataGridViewCell currentCell = dataGridView.Rows[rowIndex].Cells[column.Index];
currentCell.Value = dic[name];
}
}
}
}
簡單演示,當然我這里沒判定他是否為null,自己加上
uj5u.com熱心網友回復:
學習一下 express<func<>>或者 func() ,action()等等express<func<>> 構建運算式樹。
普通的記憶體直接操作資料,可以用 func
下圖演示了,
1 設定 Func的條件
2 在myList中呼叫 where,添加1的條件

uj5u.com熱心網友回復:
方法很好,如果你再說你的復雜,請上設計模式,我就不信了你。。。。。
uj5u.com熱心網友回復:
其實99.99%的實際專案情況下是不需要“逆向”去考慮什么“萬能解決方案”的,而是要提供一個快速開發工具。你寫一個能理解程式員的“意向”的程式,只要程式員大腦里一有想法,程式就把代碼撰寫出來,而且測驗報告都列印出來了。爽不爽?假不假?實用不實用?
uj5u.com熱心網友回復:
可以用func<>委托或者expression<func<>>運算式樹uj5u.com熱心網友回復:
其實大多數程式員,80%的時間都在消磨時光,做各種不真實的“研發”。當然這就是現實,假設大多數程式員能把80%的時間干正確的事情,那么編程行業也就不存在了。傻子不存在了,也就沒人能靠雇傭程式員來賺錢了。uj5u.com熱心網友回復:
作為管理者其實清楚風險。有些東西只有搞技術的人“自以為”很酷。真正的實踐者看大趨勢來判斷這種東西是不是特定人該考慮的事情就能判斷,看能不能保證編譯時排除錯誤、測驗時發現盡可能多的邏輯錯誤,不要等到未來系統上線了之后再因為所謂“萬能隱藏錯誤”而在用戶面前出問題。uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
樓上都是大神,如下代碼第19行編譯錯誤:class AttributeTag
{
public Expression<Func<Dictionary<string, Dictionary<string, ExtendedAttribute>>, bool>> Filter { get; set; }
//...
}
class JobInfo
{
public static Dictionary<InterfaceType, Dictionary<string, Dictionary<string, ExtendedAttribute>>> AllAttrs { get; set; }
//...
}
private void Test()
{
var dic = JobInfo.AllAttrs[InterfaceType.PROCESS];
attributeTag = new AttributeTag
{
Filter = dic.Where(d => d.Key.StartsWith("Final") || d.Key.StartsWith("Sub") || d.Key.StartsWith("Buired")),
InterfaceType = InterfaceType.PROCESS
};
dgvResinPlug.Tag = attributeTag;
}
第19行提示錯誤,請問如何修改:
無法將型別
“System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, System.Collections.Generic.Dictionary<string, EntryForm.Models.ExtendedAttribute>>>”
隱式轉換為
“System.Linq.Expressions.Expression<System.Func<System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, EntryForm.Models.ExtendedAttribute>>, bool>>”
uj5u.com熱心網友回復:
test(func<string,bool> func)
{
dataGridView.Rows.Clear();
foreach (var name in dic.Keys)
{
if (func(name)) //此處應如何設計滿足不同的需求?
{
int rowIndex = dataGridView.Rows.Add();
foreach (DataGridViewColumn column in dataGridView.Columns)
{
DataGridViewCell currentCell = dataGridView.Rows[rowIndex].Cells[column.Index];
currentCell.Value = dic[name];
}
}
}
}
簡單演示,當然我這里沒判定他是否為null,自己加上
學習一下 express<func<>>或者 func() ,action()等等
express<func<>> 構建運算式樹。
普通的記憶體直接操作資料,可以用 func
下圖演示了,
1 設定 Func的條件
2 在myList中呼叫 where,添加1的條件
兩位大神,可否看看上面9樓的代碼應該如何修改,謝謝
uj5u.com熱心網友回復:
兩位大神,可否看看上面9樓的代碼應該如何修改,謝謝
wo簡化了一下,你照著改改
Expression<Func<Dictionary<string, Dictionary<string, string>>, bool>> Filter;
Dictionary<string, Dictionary<string, Dictionary<string, string>>> AllAttrs=new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
Filter = (x) => AllAttrs[""].Where(d => d.Key.StartsWith("Final")) == x;
uj5u.com熱心網友回復:
可以把條件和執行的代碼發入資料庫表里,通過查詢SQL獲取對應執行代碼。 小型監控攝像頭轉載請註明出處,本文鏈接:https://www.uj5u.com/net/29688.html
標籤:C#
上一篇:C# 班長五人競選,考慮平票。
下一篇:byte型別求解
