是否有可能在VBScript中获得.Net字符串对象实例?

Aut*_*aos 7 .net string com vbscript

在VBScript中,您可以使用COM自动化使用某些.net类.当您想要使用动态数组,列表,队列等时,这会派上用场.

这将是很好,如果我可以使用字符串作为对象,所以我能做的所有花哨的字符串的东西吧,但每当我从另一个对象传递一个字符串,它是由VBScript中被视为一个字符串,而不是作为一个字符串对象:

Set s = CreateObject("System.Text.StringBuilder")
s.Append_3 "I love deadlines. I like the whooshing sound they make as they fly by."

' This gives me the literal string
MsgBox s.ToString
text = s.ToString

' But unfortunately this won't work
MsgBox s.ToString.Length
Set stringRef = s.ToString
Run Code Online (Sandbox Code Playgroud)

另外,将字符串创建为COM对象也不起作用:

Set s = CreateObject("System.String")      ' Nope.
Run Code Online (Sandbox Code Playgroud)

是否有人管理过这个,或者对此有其他想法?

pet*_*ter 7

您可以使用某些方法和属性,但不是全部.以下工作,但从您使用toString的那一刻起,您就有了一个行为相同的vbscript变量.

Set s = CreateObject("System.Text.StringBuilder")
s.Append_3 "I love deadlines. I like the whooshing sound they make as they fly by."
s.Append_3 "and the rest."
wscript.echo s.Length
wscript.echo s.Capacity
wscript.echo chr(s.chars(0))
wscript.echo s.Replace("t", "d").Replace("l", "k").toString
Run Code Online (Sandbox Code Playgroud)

83
140
I
I kove deadkines. I kike dhe whooshing sound dhey make as dhey fky by.and dhe resd.
Run Code Online (Sandbox Code Playgroud)

但是例如下面的方法不起作用,虽然它是一个stringbuilder的方法http://msdn.microsoft.com/en-us/library/system.text.stringbuilder_methods.aspx 不要问我为什么

s.Insert 1, "insert this"
Run Code Online (Sandbox Code Playgroud)

s.Insert_2 7, "insert this"
Run Code Online (Sandbox Code Playgroud)

确实有效

我也在Ruby中编程,你也可以在这里使用这些对象,它也是同样的行为.对于某些对象,我可以枚举属性或方法,例如Excel

require 'win32ole'
excel = WIN32OLE.new('Excel.Application')
properties = excel.ole_get_methods
properties.each do |property|
  p property.to_s
end
Run Code Online (Sandbox Code Playgroud)

给出了很长的列表

"Application"
"Creator"
"Parent"
"ActiveCell"
"ActiveChart"
"ActiveDialog"
"ActiveMenuBar"
"ActivePrinter"
"ActiveSheet"
"ActiveWindow"
"ActiveWorkbook"
etc etc
Run Code Online (Sandbox Code Playgroud)

但对于System.Text.Stringbuilder来说并非如此,我想这是由于程序员将他的方法和属性暴露给外部的方式.

遗憾的是,我认为在vbscript中直接使用System.String是不可能的.