今天,在与我的同事交谈时,脑子里出现了一些奇怪的东西.
处理来自vb6的字符串的"秘密"方式,如:
Dim strSomeString as String
strSomeString = "i am phat"
Mid$(strSomeString, 6,4) = "hack"
Run Code Online (Sandbox Code Playgroud)
这将放在i am hack里面strSomeString.
虽然对vb6中支持的这种奇怪感到惊讶,但当我读到它在VB.Net中也得到支持时(我可能与旧代码的兼容性),我完全被吹了.
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"
Run Code Online (Sandbox Code Playgroud)
我的问题是:它在技术上如何运作?什么是Mid像个在特定的情况呢?(方法?函数?扩展方法?)
它被转换为Microsoft.VisualBasic.CompilerServices.StringType中对此函数的MSIL调用
Public Shared Sub MidStmtStr ( _
ByRef sDest As String, _
StartPosition As Integer, _
MaxInsertLength As Integer, _
sInsert As String _
)
Run Code Online (Sandbox Code Playgroud)
这个编译器技巧纯粹是为了向后兼容.它内置于编译器中,因此不是您可以在自己的类型上实现的技巧.
所以
Mid(TestString, 5, 3) = "fox"
Run Code Online (Sandbox Code Playgroud)
变
MidStmtStr(TestString, 5, 3, "fox")
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助,
| 归档时间: |
|
| 查看次数: |
417 次 |
| 最近记录: |