How do I call DotNetFactory from VBScript in a stand-alone .vbs file?

谁说我不能喝 提交于 2019-12-12 12:34:42

问题


I've been exploring options for expanding my QuickTest Professional scripting capabilities, and came across this article this morning, so I decided to experiment a bit. The code below works fine when executed inside the QTP environment, but I could see a use for this outside of the QTP environment as well. Unfortunately, it is causing an error when run from a stand-alone .vbs file

Set MyDate = DotNetFactory.CreateInstance("System.DateTime").Now
msgbox MyDate.ToShortDateString()

The error is "Object Required: 'DotNetFactory'"

I've made .Net calls from VBScript before, but none of them have used DotNetFactory. Code such as

Set coll = CreateObject("System.Collections.Queue")

Does not cause an error.

Do I need to set a reference to DotNetFactory? The text from the article

We use ‘System.DateTime’ as type name. We do not need to specify the assembly for this as it belongs to the already loaded namespace ‘System’ (mscorlib.dll).

makes me think so, because nothing is automatically loaded by my script editor. If so, how do I do this? I am not using an IDE, just a text editor, so any references would have to be loaded by the script itself.

Update: As pointed out by Motto, it can't be done without some extra work. The quote from the article was pointing out that System.DateTimenot DotNetFactory is included in mscorlib.


回答1:


AFAIK DotNetFactory is an object created by QTP, not part of mscorelib as you said in a comment to Mikeb's answer. Therefore you can't access it from a stand along VBS file unless QTP has exposed a prog-id.




回答2:


Can you create a DotNetFactory, as in :

Set dnf = CreateObject("Qualified.Name.To.DotNetFactory")
dnf.CreateInstance("System.DateTime")


来源:https://stackoverflow.com/questions/1986078/how-do-i-call-dotnetfactory-from-vbscript-in-a-stand-alone-vbs-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!