表达式能否、如何作为参数传递

leon51 2020-06-17 10:26:31
在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];
}
}
}
}
...全文
417 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39080073 2020-06-22
  • 打赏
  • 举报
回复
可以把条件和执行的代码发入数据库表里,通过查询SQL获取对应执行代码。 小型监控摄像头
正怒月神 2020-06-17
  • 打赏
  • 举报
回复
引用 3 楼 mingcsharp 的回复:
[quote=引用 2 楼 正怒月神 的回复:] 学习一下 express<func<>>或者 func() ,action()等等 express<func<>> 构建表达式树。 普通的内存直接操作数据,可以用 func 下图演示了, 1 设定 Func的条件 2 在myList中调用 where,添加1的条件
方法很好,如果你再说你的复杂,请上设计模式,我就不信了你。。。。。[/quote]
  • 打赏
  • 举报
回复
作为管理者其实清楚风险。有些东西只有搞技术的人“自以为”很酷。真正的实践者看大趋势来判断这种东西是不是特定人该考虑的事情就能判断,看能不能保证编译时排除错误、测试时发现尽可能多的逻辑错误,不要等到未来系统上线了之后再因为所谓“万能隐藏错误”而在用户面前出问题。
  • 打赏
  • 举报
回复
其实大多数程序员,80%的时间都在消磨时光,做各种不真实的“研发”。当然这就是现实,假设大多数程序员能把80%的时间干正确的事情,那么编程行业也就不存在了。傻子不存在了,也就没人能靠雇佣程序员来赚钱了。
threenewbee 2020-06-17
  • 打赏
  • 举报
回复
可以用func<>委托或者expression<func<>>表达式树
  • 打赏
  • 举报
回复
其实99.99%的实际项目情况下是不需要“逆向”去考虑什么“万能解决方案”的,而是要提供一个快速开发工具。

你写一个能理解程序员的“意向”的程序,只要程序员大脑里一有想法,程序就把代码编写出来,而且测试报告都打印出来了。爽不爽?假不假?实用不实用?
mingcsharp 2020-06-17
  • 打赏
  • 举报
回复
引用 2 楼 正怒月神 的回复:
学习一下 express<func<>>或者 func() ,action()等等 express<func<>> 构建表达式树。 普通的内存直接操作数据,可以用 func 下图演示了, 1 设定 Func的条件 2 在myList中调用 where,添加1的条件
方法很好,如果你再说你的复杂,请上设计模式,我就不信了你。。。。。
正怒月神 2020-06-17
  • 打赏
  • 举报
回复
学习一下 express<func<>>或者 func() ,action()等等 express<func<>> 构建表达式树。 普通的内存直接操作数据,可以用 func 下图演示了, 1 设定 Func的条件 2 在myList中调用 where,添加1的条件
wanghui0380 2020-06-17
  • 打赏
  • 举报
回复
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,自己加上
正怒月神 2020-06-17
  • 打赏
  • 举报
回复
引用 10 楼 leon51 的回复:
两位大神,可否看看上面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;
leon51 2020-06-17
  • 打赏
  • 举报
回复
引用 1 楼 wanghui0380 的回复:
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,自己加上
引用 2 楼 正怒月神 的回复:
学习一下 express<func<>>或者 func() ,action()等等 express<func<>> 构建表达式树。 普通的内存直接操作数据,可以用 func 下图演示了, 1 设定 Func的条件 2 在myList中调用 where,添加1的条件
两位大神,可否看看上面9楼的代码应该如何修改,谢谢
leon51 2020-06-17
  • 打赏
  • 举报
回复
楼上都是大神,如下代码第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>>”

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧