首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 發現之旅第十四講:VB.NET 腳本引擎(上)─ Execute 執行腳本與 Window 全域物件架構

     編輯:關於C語言

Execute 核心執行邏輯

支援拋出例外 (ThrowException=true) 或靜默失敗 (false) 兩種模式:


public object Execute(string methodName, object[] parameters, bool throwException) {
    if (!CheckReady()) return null;
    if (throwException) {
        if (methodName == null) throw new ArgumentNullException("MethodName");
        methodName = methodName.Trim();
        if (methodName.Length == 0) throw new ArgumentException("MethodName");
    } else {
        if (methodName == null) return null;
        methodName = methodName.Trim();
        if (methodName.Length == 0) return null;
    }
    foreach (ScriptMethodInfo info in myScriptMethods) {
        if (string.Compare(info.MethodName, methodName, true) == 0) {
            try {
                object result = info.MethodDelegate != null 
                    ? info.MethodDelegate.DynamicInvoke(parameters)
                    : info.MethodObject.Invoke(null, parameters);
                return result;
            } catch (Exception ex) {
                if (throwException) throw;
                Console.WriteLine($"VBA:{methodName}:{ex.Message}");
                return null;
            }
        }
    }
    return null;
}

輔助方法

  • HasMethod(name):檢查方法是否存在
  • ExecuteSub(name):安全執行無參數方法 (Execute(name, null, false))

Window 全域物件架構

參考 JavaScript window 物件設計 XVBAWindowObject,核心成員分類:

  1. 螢幕/視窗屬性:ScreenWidth/Height、Title/Left/Top/Width/Height
  2. 排程方法:SetTimeout/ClearTimeout/SetInterval/ClearInterval
  3. 互動方法:Alert/AlertError/Confirm/Prompt/BrowseFile/BrowseFolder

核心屬性定義


protected bool bolUserInteractive = true;
public bool UserInteractive { get => bolUserInteractive; set => bolUserInteractive = value; }

protected string strSystemName = "應用程式";
public string SystemName { get => strSystemName; set => strSystemName = value; }

public XVBAEngine Engine { get => myEngine; }

public IWin32Window ParentWindow { get => myParentWindow; set => myParentWindow = value; }

public int ScreenWidth => bolUserInteractive ? Screen.PrimaryScreen.Bounds.Width : 0;
public int ScreenHeight => bolUserInteractive ? Screen.PrimaryScreen.Bounds.Height : 0;

ParentWindow 為宿主主視窗,WinForm 專案需指派以啟用 UI 功能。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved