C# 中的谓词问题

发布于 2024-11-19 07:48:42 字数 572 浏览 2 评论 0原文

我有以下方法定义(已编辑以删除多余的泛型):

public static T SearchAgaistValues<T>(Dictionary<string, string> input, 
string key, List<T> values, Predicate<T> match, out string[] cmdParams)

我的简化要求如下。我需要在 input 中搜索 key,如果找到,请查看其值是否出现在 values 中。但是,values 是通用的(并且显然会包含我需要匹配的字符串)。因此,在我看来,我必须传递一个谓词方法来执行匹配。

然而,我见过的每个 Predicate示例都有一个硬编码的比较器。我需要将找到的值与中的每个项目进行比较。但是,我无法传递这些值。

我不知道如何使用基于委托的匹配方法在 foreach 循环之外执行此操作。

我在这里错过了什么吗?

I have the following method definition (EDITED to remove redundant generic):

public static T SearchAgaistValues<T>(Dictionary<string, string> input, 
string key, List<T> values, Predicate<T> match, out string[] cmdParams)

My simplified requirements are as follows. I need to search input for key, and if found, see if its value appears in values. However, values is generic (and will obviously contain a string that I need to match). Therefore, the way I see it, I have to pass a predicate method to perform the matching.

However, every example of Predicate<T> I have seen has a hard coded comparitor. I need to compare the found key's value to each item in values. I cannot pass these values, however.

I can't see how to do this outside of a foreach loop with a delegate based match method.

Am I missing something here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香草可樂 2024-11-26 07:48:42

在我看来,你有两种选择,而无需改变疯狂的要求。

选项 1 是使用 Func 而不是 Predicate。这样谓词就可以根据需要在 string 和 T1 之间进行转换并返回布尔匹配结果。

public static T1 SearchAgaistValues<T, T1>(
            Dictionary<string, string> input, 
            string key, 
            List<T1> values, 
            Func<string, T1, bool> match, 
            out string[] cmdParams)

或者,您可以传递额外的 Converter 参数,将查找的字符串转换为 T1,然后使用谓词进行比较。

public static T1 SearchAgaistValues<T, T1>(
            Dictionary<string, string> input, 
            string key, 
            List<T1> values, 
            Converter<T1, string> converter,
            Predicate<T1> match, 
            out string[] cmdParams)

但这两种情况都不太理想。这个函数听起来更像是一个寻找解决方案的问题,而不是相反。签名有点疯狂,似乎可以通过重述需求或将其分解成碎片来大大简化。

As I see it you have two options, without changing crazy requirements.

Option 1 is to use Func<string, T1, bool> instead of Predicate<T1>. This way the predicate can convert between string and T1 as needed and return the boolean matched result.

public static T1 SearchAgaistValues<T, T1>(
            Dictionary<string, string> input, 
            string key, 
            List<T1> values, 
            Func<string, T1, bool> match, 
            out string[] cmdParams)

Alternatively you can pass an additional Converter<T1, string> parameter to convert the looked-up string to a T1 and then compare using the predicate.

public static T1 SearchAgaistValues<T, T1>(
            Dictionary<string, string> input, 
            string key, 
            List<T1> values, 
            Converter<T1, string> converter,
            Predicate<T1> match, 
            out string[] cmdParams)

Both cases are less than ideal though. This function sounds a lot more like a problem looking for a solution than the other way around. The signature is a bit crazy and seems like it can be greatly simplified by restating the requirements or breaking it up into pieces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文