gpt4 book ai didi

vb.net - VB Like 运算符与 Operators.LikeString() 不同

转载 作者:行者123 更新时间:2023-12-04 07:02:35 26 4
gpt4 key购买 nike

为什么在下面的示例中,第一个 (x) 为真,第二个 (y) 为假?
我想在 C# 中使用 VB-Like 运算符。

Dim x = "abc@symas-design.ch" Like "*symas-design.ch"
Dim y = Microsoft.VisualBasic.CompilerServices.Operators.LikeString("abc@symas-design.ch", "*symas-design.ch", CompareMethod.Text)

最佳答案

Like运营商显然不使用 Operators.LikeString ,而是 LikeOperator.LikeString .

我试过这个 LINQPad程序:

Sub Main
Dim x = "abc@symas-design.ch" Like "*symas-design.ch"
Dim y = Microsoft.VisualBasic.CompilerServices.Operators.LikeString("abc@symas-design.ch", "*symas-design.ch", Microsoft.VisualBasic.CompareMethod.Text)

x.Dump()
y.Dump()
End Sub

它显示:
True
False

所以正如你所说,他们不这样做,但后来我展示了生成的 IL:
IL_0000:  ldstr       "abc@symas-design.ch"
IL_0005: ldstr "*symas-design.ch"
IL_000A: ldc.i4.0
IL_000B: call Microsoft.VisualBasic.CompilerServices.LikeOperator.LikeString
IL_0010: stloc.0 // x
IL_0011: ldstr "abc@symas-design.ch"
IL_0016: ldstr "*symas-design.ch"
IL_001B: ldc.i4.1
IL_001C: call Microsoft.VisualBasic.CompilerServices.Operators.LikeString
IL_0021: stloc.1 // y
IL_0022: ldloc.0 // x
IL_0023: call LINQPad.Extensions.Dump
IL_0028: pop
IL_0029: ldloc.1 // y
IL_002A: call LINQPad.Extensions.Dump

似乎他们正在调用不同的方法,所以我尝试了这个:
Sub Main
Dim x = "abc@symas-design.ch" Like "*symas-design.ch"
Dim y = Microsoft.VisualBasic.CompilerServices.Operators.LikeString("abc@symas-design.ch", "*symas-design.ch", Microsoft.VisualBasic.CompareMethod.Text)
Dim z = Microsoft.VisualBasic.CompilerServices.LikeOperator.LikeString("abc@symas-design.ch", "*symas-design.ch", Microsoft.VisualBasic.CompareMethod.Text)

x.Dump()
y.Dump()
z.Dump()
End Sub

我得到了:
True   <-- Like operator
False <-- Operator.LikeString
True <-- LikeOperator.LikeString

所以你需要使用 LikeOperator.LikeString反而。

这是一个 C# LINQPad程序演示:
void Main()
{
bool z = LikeOperator.LikeString("abc@symas-design.ch", "*symas-design.ch", Microsoft.VisualBasic.CompareMethod.Text);
z.Dump();
}

它还输出 True .

关于vb.net - VB Like 运算符与 Operators.LikeString() 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20927282/

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