如何扩展 Func 委托以包含超过最多四个参数?
我一直在使用 LINQ 进行编译查询,基本上使用 Func
传递到编译查询,但问题是它最多有四个参数。
扩展这个是好的做法吗?
有什么方法可以扩展这个或者我应该创建自己的委托吗?
有时我需要传递 6 个参数,其他参数为 5 个,其他参数为 4 个或更少......因此,如果有 4 个或更少参数,我可以继续使用 Func
委托。
目前传递的数据上下文和我需要的参数组合取决于每个单独的编译查询。
I have been using LINQ with compiled queries, basically passing into the compiled query using Func
but the problem is that it has a maximum of four parameters.
Is it good practice to extend this?
Is there any way to extend this or should I create my own delegate?
Sometimes I need to pass six params and others five and others four or less... so with four or less I could continue using the Func
delegate.
Currently passing in data context and a mix of the params I need are dependent on each individual compiled query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需声明您自己的委托 - 只需一行代码即可完成此操作。请参阅我在 .NET 2.0 中声明 .NET 3.5 委托的示例以获得某种模板,如果你需要的话。
我不知道这与 LINQ 编译查询的配合效果如何 - 希望这不会成为问题,但您的用例还不够清晰,我无法肯定地说。
请注意,在 .NET 4.0 中支持 Func /Action 最多有 8 个参数。
Just declare your own delegates - it's only a single line of code to do so. See my examples for declaring the .NET 3.5 delegates in .NET 2.0 for a sort of template, if you need it.
I don't know how well this will work with LINQ compiled queries - hopefully it won't be a problem, but your use case isn't quite clear enough to me to say for sure.
Note that in .NET 4.0 there's support for Func/Action with up to 8 parameters.
乔恩所说的加上...
正如您所建议的,通常您不应该以太多参数结束 - 通常您应该发现一些统一的概念跳出来告诉您 引入参数对象。话虽如此,4 不会是我放置“这太疯狂”这句话的地方。
What Jon said plus...
As you've suggested, generally you shouldnt end up with too many params - generally you should find some uniting concept jumping out telling you to Introduce Parameter Object. Having said that, 4 wouldnt be the place where I'd place a 'that's just crazy' line.