VB.NET中Mid作为左操作符为何表现异常(令人困惑?)

2026-05-06 08:301阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计354个文字,预计阅读时间需要2分钟。

VB.NET中Mid作为左操作符为何表现异常(令人困惑?)

今天,在与你的交谈中,我脑海中涌现了一些奇怪的东西。处理来自VB6的字符串秘密方式,如下:

Dim strSomeString As StringstrSomeString=i am phatMid$(strSomeString, 6, 4)=hack这会使我在str中秘密

今天,在与我的同事交谈时,脑子里出现了一些奇怪的东西.

VB.NET中Mid作为左操作符为何表现异常(令人困惑?)

处理来自vb6的字符串的“秘密”方式,如:

Dim strSomeString as String strSomeString = "i am phat" Mid$(strSomeString, 6,4) = "hack"

这会让我在strSomeString中进行攻击.

虽然对vb6中支持的这种奇怪感到惊讶,但当我读到it is supported in VB.Net too(可能与旧代码的兼容性)时,我完全被吹了.

Dim TestString As String ' Initializes string. TestString = "The dog jumps" ' Returns "The fox jumps". Mid(TestString, 5, 3) = "fox" ' Returns "The cow jumps". Mid(TestString, 5) = "cow" ' Returns "The cow jumpe". Mid(TestString, 5) = "cow jumped over" ' Returns "The duc jumpe". Mid(TestString, 5, 3) = "duck"

我的问题是:它在技术上如何运作?在那种特殊情况下,Mid的表现如何? (方法?函数?扩展方法?)

它被转换为Microsoft.VisualBasic.CompilerServices.StringType中对此函数的MSIL调用

Public Shared Sub MidStmtStr ( _ ByRef sDest As String, _ StartPosition As Integer, _ MaxInsertLength As Integer, _ sInsert As String _ )

这个编译器技巧纯粹是为了向后兼容.它内置于编译器中,因此不是您可以在自己的类型上实现的技巧.

所以

Mid(TestString, 5, 3) = "fox"

MidStmtStr(TestString, 5, 3, "fox")

希望这可以帮助,

本文共计354个文字,预计阅读时间需要2分钟。

VB.NET中Mid作为左操作符为何表现异常(令人困惑?)

今天,在与你的交谈中,我脑海中涌现了一些奇怪的东西。处理来自VB6的字符串秘密方式,如下:

Dim strSomeString As StringstrSomeString=i am phatMid$(strSomeString, 6, 4)=hack这会使我在str中秘密

今天,在与我的同事交谈时,脑子里出现了一些奇怪的东西.

VB.NET中Mid作为左操作符为何表现异常(令人困惑?)

处理来自vb6的字符串的“秘密”方式,如:

Dim strSomeString as String strSomeString = "i am phat" Mid$(strSomeString, 6,4) = "hack"

这会让我在strSomeString中进行攻击.

虽然对vb6中支持的这种奇怪感到惊讶,但当我读到it is supported in VB.Net too(可能与旧代码的兼容性)时,我完全被吹了.

Dim TestString As String ' Initializes string. TestString = "The dog jumps" ' Returns "The fox jumps". Mid(TestString, 5, 3) = "fox" ' Returns "The cow jumps". Mid(TestString, 5) = "cow" ' Returns "The cow jumpe". Mid(TestString, 5) = "cow jumped over" ' Returns "The duc jumpe". Mid(TestString, 5, 3) = "duck"

我的问题是:它在技术上如何运作?在那种特殊情况下,Mid的表现如何? (方法?函数?扩展方法?)

它被转换为Microsoft.VisualBasic.CompilerServices.StringType中对此函数的MSIL调用

Public Shared Sub MidStmtStr ( _ ByRef sDest As String, _ StartPosition As Integer, _ MaxInsertLength As Integer, _ sInsert As String _ )

这个编译器技巧纯粹是为了向后兼容.它内置于编译器中,因此不是您可以在自己的类型上实现的技巧.

所以

Mid(TestString, 5, 3) = "fox"

MidStmtStr(TestString, 5, 3, "fox")

希望这可以帮助,