如何使用MSScriptControl检查脚本是否定义了特定的类或属性?

时间:2015-06-24 03:16:39

标签: vb6 msscriptcontrol

我目前使用MSScriptControl的遗留VB6程序是白痴的。它将文件名作为输入,然后获取在所述文件中定义的Property的{​​{1}}中定义的一些字符串。

这是当前的代码:

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()的全部目的是通过实例化ScriptControl并使用来获取文件的类属性PropStringAPropStringB中定义的字符串Class

这是我的问题:这个(黑客)有效,但是有更好,更精致的方法让我检查输入脚本实际上是否包含我需要的东西,特别是ScriptControl.Eval定义和Class(ies)?如果文件不是空白但实际上不包含VB代码,比如文本文件呢?

额外奖励:我应该在CodeReview代替吗?

0 个答案:

没有答案