gpt4 book ai didi

Convert string to C# Conditional operator [closed](将字符串转换为C#条件运算符[Closed])

转载 作者:bug小助手 更新时间:2023-10-25 19:54:41 25 4
gpt4 key购买 nike




I'd like convert string to ConditionalOperator, few sample :

我想将字符串转换为ConditionalOperator,几个示例:



  • "and" to &&

  • "or" to ||

  • "greatherthan" to >=

  • "contains" to .Contains


Something like this :

大概是这样的:


public static class Extension
{
public static Operator ConditionalOperator(this string logic)
{
switch (logic)
{
case "and": return // return && conditionaloperator
case "or": return // return || conditionaloperator
case "greatherthan": return // return >= conditionaloperator
}
}
}

The second argument is not all the time a string but can be int, double, bool, ...

第二个参数并不总是字符串,但可以是int、Double、bool、...


I'd like build something like : ConditionalOperator("==", "StringToSearch") return a Func<>

我希望构建如下内容:ConditionalOperator(“==”,“StringToSearch”)返回一个Func<>


Do you have an idea how to do this ?

你知道怎么做吗?


Thanks,

谢谢,


Update 1, to clarify:
I received a list of object "Filter"enter image description here

更新1,澄清:我收到了对象列表“过滤器”


The "FilterOperator" all possible values for a CondtionalOperator.

“FilterOperator”CondtionalOperator的所有可能值。


更多回答

What is Operator or Opertor? What is the second argument?

什么是操作员或操作员?第二个论点是什么?

I think this post might be of help to you.

我想这篇文章可能会对你有所帮助。

@StephenRoss I read this post but I don't understand where "Operator" comes from Operator.LessThan<T>;

@斯蒂芬罗斯我读了这篇帖子,但我不明白“操作员”从何而来。

@shingo it's a typo

@Shingo这是个打字错误

How would you do Contains with an int? Or a bool?

你会如何用一个整型来处理包含呢?还是一只野猪?

优秀答案推荐

You can use generics

您可以使用泛型


public static bool Operator(string logic, T x, T y) where T : IComparable<T>
{
var compare = Comparer<T>.Default.Compare(x, y);
return logic switch
{
">" => compare > 0,
"<" => compare < 0,
"==" => compare == 0,
_ => throw new Exception("invalid logic"),
};
}

Optionally you could pass an explicit IComparer

或者,您可以传递一个显式的ICompeller


public static bool Operator(string logic, T x, T y, IComparer<T> comparer)
{
var compare = comparer.Compare(x, y);
return logic switch
{
">" => compare > 0,
"<" => compare < 0,
"==" => compare == 0,
_ => throw new Exception("invalid logic"),
};
}

To return a Func<T, T, bool> you can just do

要返回Func ,您只需执行以下操作


public static Func<T, T, bool> Operator(string logic, T x, T y, IComparer<T> comparer)
{
return logic switch
{
">" => (x, y) => comparer.Compare(x, y) > 0,
"<" => (x, y) => comparer.Compare(x, y) < 0,
"==" => (x, y) => comparer.Compare(x, y) == 0,
_ => throw new Exception("invalid logic"),
};
}

For string.Contains you should probably make a separate overload just for string, as that cannot be genericized.

包含您可能应该只为字符串创建一个单独的重载,因为它不能泛化。



public static class Extension
{
public static Boolean Operator(this string logic, int x, int y)
{
switch (logic)
{
case ">": return x > y;
case "<": return x < y;
case "==": return x == y;
default: throw new Exception("invalid logic");
}
}
}

Source

来源


更多回答

if there is Contains for a string ?

如果一个字符串有包含呢?

I don't understand your question. Yes there is a Contains function, but that's not what you asked, and it wouldn't make sense to use that with generics. Perhaps you should make a separate overload just for string.

我不明白你的问题。是的,确实有一个包含函数,但这不是您所要求的,在泛型中使用它是没有意义的。也许您应该为字符串创建一个单独的重载。

I updated the question to clarify. It's more create an expression.

我更新了问题以澄清。更重要的是创造一种表达方式。

I saw this code but it's not all the time int but can be string, double, long, ...

我看到了这段代码,但它并不总是int,而是可以是字符串、双精度、长整型...

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com