加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

VB.net Mid作为左运算符特殊(奇怪?)行为

发布时间:2020-12-17 07:22:03 所属栏目:百科 来源:网络整理
导读:今天,在与我的同事交谈时,脑子里出现了一些奇怪的东西. 处理来自vb6的字符串的“秘密”方式,如: Dim strSomeString as String strSomeString = "i am phat" Mid$(strSomeString,6,4) = "hack" 这会让我在strSomeString中进行攻击. 虽然对vb6中支持的这种奇
今天,在与我的同事交谈时,脑子里出现了一些奇怪的东西.

处理来自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,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,3) = "fox"

MidStmtStr(TestString,3,"fox")

希望这可以帮助,

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读