我目前正在对使用MSScriptControl
. 它以文件名作为输入,然后获取在所述文件中定义的Property
a 中定义的一些字符串。Class
这是当前代码:
Dim oScript As New ScriptControl
With oScript
.Language = "VBSCRIPT"
.Reset
.UseSafeSubset = False
.Timeout = -1
sScriptCode = "ScriptLoadedFromSomeFileDefinedSomewhereElse" 'This variable contains the entire length of the script file
'This Len() should guard against Test:FileIsBlank
If Len(sScriptCode) = 0 Then GoTo DoTheRest
'This InStr() should guard against Test:FileDoesNotContainClassDef
If InStr(1, sScriptCode, "Class myClassName", vbTextCompare) = 0 Then GoTo DoTheRest
.AddCode sScriptCode
.AddCode "Dim myClassName"
.AddCode "Set myClassObject = New myClassName"
'These InStr()s should guard against Test:FileDoesNotContainExpectedSubs
If InStr(1, sScriptCode, "Property Get PropStringA", vbTextCompare) = 0 Then GoTo DoTheRest
sPropStringA = .Eval("myClassObject.PropStringA")
If InStr(1, sScriptCode, "Property Get PropStringB", vbTextCompare) = 0 Then GoTo DoTheRest
sPropStringB = .Eval("myClassObject.PropStringB")
'This should guard against everything else. That is, Test:FileContainsInvalidVBCode
If Err Then GoTo DoTheRest
End With
DoTheRest:
'Rest of Code
这是输入脚本文件的相关部分:
Class myClassName
Public Property Get PropStringA()
PropStringA = _
"StringA"
End Property
Public Property Get PropStringB()
PropStringB = _
"StringB,StringC,StringD,StringE"
End Property
以上内容通过 加载到sScriptCode
变量中FileSystemObject.OpenTextFile()
。
using 的全部目的是通过实例化和 usingScriptControl
来获取文件的 Class PropertiesPropStringA
和中定义的字符串。PropStringB
Class
ScriptControl.Eval
这是我的问题:这个(hack)有效,但有没有更好、更精致的方法让我检查输入脚本是否真的包含我需要的东西,特别是Class
定义和Public Get Property
(ies)?如果文件不是空白但实际上不包含 VB 代码,比如文本文件,那该怎么办?
奖励:我应该在CodeReview上问这个吗?