有条件调用 StartsWith/EndsWith 的最佳方法

发布于 2024-12-27 00:54:05 字数 532 浏览 2 评论 0原文

我有一个类似于以下的方法:

StringComparison comparison = StringComparison.CurrentCultureIgnoreCase;

switch(SearchType)
{
case SearchType.Contains:
list = list.Where(a => a.Reference.Contains("test",comparison));
break;

case SearchType.StartsWith:
list = list.Where(a => a.Reference.StartsWith("test",comparison));
break;

case SearchType.EndsWith:
list = list.Where(a => a.Reference.EndsWith("test",comparison));
break;
}

您可能会猜到 SearchType 是我的自定义枚举。

有没有更简单的方法来做到这一点,可能使用反射?多个开关看起来有点难看。

I have a method similar to the following:

StringComparison comparison = StringComparison.CurrentCultureIgnoreCase;

switch(SearchType)
{
case SearchType.Contains:
list = list.Where(a => a.Reference.Contains("test",comparison));
break;

case SearchType.StartsWith:
list = list.Where(a => a.Reference.StartsWith("test",comparison));
break;

case SearchType.EndsWith:
list = list.Where(a => a.Reference.EndsWith("test",comparison));
break;
}

As you can probably guess SearchType is a custom enum I have.

Is there an easier way of doing this, possibily using reflection? The multiple switches seem a bit ugly.

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

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

发布评论

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

评论(3

话少情深 2025-01-03 00:54:05

哪一部分不容易?

我想您可以为每个枚举创建一个类,从通用的 SearchType 接口继承并实现一个名为 ProcessList 的函数 - 不确定哪个列表可以提供更好的功能。

就像......

interface SearchType
{
   object ProcessList(object list, string text);
}

class Contains : SearchType
{
   object ProcessList(object list, string text)
   {
      list = list.Where(a => a.Reference.Contains(text, StringComparison.CurrentCultureIgnoreCase));
   }
}

需要为每个枚举类型做一个类。

然后您需要像这样设置 SearchType 变量...

SearchType searchType = new Contains();//or something else

然后您的开关可以用这个替换...

list = searchType.ProcessList(list, "test");

...在编码方面并不是很容易,但您会得到一个更易读的代码而不是开关。

Which part of that is not easy?

I guess you could have a class for each enum instead, inherit from a common SearchType interface and implement a function called ProcessList - not sure what list is to offer a better function.

Something like..

interface SearchType
{
   object ProcessList(object list, string text);
}

class Contains : SearchType
{
   object ProcessList(object list, string text)
   {
      list = list.Where(a => a.Reference.Contains(text, StringComparison.CurrentCultureIgnoreCase));
   }
}

Need to do a class for each enum type.

Then you would need to set the SearchType variable like this...

SearchType searchType = new Contains();//or something else

and your switch could then be replaced with this...

list = searchType.ProcessList(list, "test");

...Not really easier in terms of coding, but you get a more readable code instead of the switch.

冰雪梦之恋 2025-01-03 00:54:05

您可以为您的函数提供委托:

Test(Func<string, StringComparison, bool> tester)
{
    return tester("test", comparison);
}

You can supply a delegate to your function:

Test(Func<string, StringComparison, bool> tester)
{
    return tester("test", comparison);
}
情话墙 2025-01-03 00:54:05

这应该有效:

StringComparison comparison = StringComparison.CurrentCultureIgnoreCase;

var methods = new Dictionnary<SearchType, Func<string, string, bool>>();

methods.Add(SearchType.Contains, (str, pattern) => str.Contains(pattern, comparison));
methods.Add(SearchType.StartsWith, (str, pattern) => str.StartsWith(pattern, comparison));
methods.Add(SearchType.EndsWith, (str, pattern) => str.EndsWith(pattern, comparison));

var contains = methods[SearchType.Contains](list, "test");
var startswith = methods[SearchType.StartsWith](list, "test");
var endswith = methods[SearchType.EndsWith](list, "test");

但是,您真的认为它比标准 switch 语句更具可读性吗?

This should works :

StringComparison comparison = StringComparison.CurrentCultureIgnoreCase;

var methods = new Dictionnary<SearchType, Func<string, string, bool>>();

methods.Add(SearchType.Contains, (str, pattern) => str.Contains(pattern, comparison));
methods.Add(SearchType.StartsWith, (str, pattern) => str.StartsWith(pattern, comparison));
methods.Add(SearchType.EndsWith, (str, pattern) => str.EndsWith(pattern, comparison));

var contains = methods[SearchType.Contains](list, "test");
var startswith = methods[SearchType.StartsWith](list, "test");
var endswith = methods[SearchType.EndsWith](list, "test");

However, do you really think it is more readable than a standard switch statement ?

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