2009-10-10 123 views
3

我正在嘗試向我的C#應用​​程序添加插件體系結構。我選擇IronScheme作爲語言,並且因爲它建立在DLR上,所以它更易於嵌入。在C#應用程序中嵌入IronScheme

在Codeplex Wiki上,他們有following example。 我改變了它稍微:

public class PluggerInner 
{ 
    IScriptEngine scheme; 

    public PluggerInner() 
    { 
     InitScheme(); 
    } 

    private void InitScheme() 
    { 
     var domMgr = ScriptDomainManager.CurrentManager; 
     var schemePrv = new IronSchemeLanguageProvider(domMgr); 

     scheme = schemePrv.GetEngine(); 
    } 

    public void RunSchemePlugin(string fileName) 
    { 
     scheme.ExecuteFile(fileName); 
    } 

    public void RunPlugins() 
    { 
     foreach (var fl in new DirectoryInfo("../../plugins").GetFiles()) 
     { 
      if (fl.Extension == ".ss") 
      { 
       RunSchemePlugin(fl.FullName); 
      } 
     } 
    } 
} 

(這基本上是執行爲new PluggerInner().RunPlugins()

它發現我的例子.ss文件中的目錄(是的,我知道我不應該使用../..),而是拋出一個大規模錯誤在這條線:

scheme.ExecuteFile(fileName); 

我得到的例外是:

 
IronScheme.Runtime.R6RS.CompoundCondition was unhandled 
    Source="IronScheme" 
    StackTrace: 
     at IronScheme.Runtime.R6RS.Exceptions.Raise(Object obj) 
     at IronScheme.Runtime.R6RS.Exceptions.RaiseContinueable(Object obj) 
     at IronScheme.Runtime.Builtins.UndefinedError(Object sym) 
     at IronScheme.IronSchemeLanguageContext.MissingName(SymbolId name) 
     at Microsoft.Scripting.ModuleGlobalWrapper.GetCachedValue() 
     at Microsoft.Scripting.ModuleGlobalWrapper.get_CurrentValue() 
     at hello.Initialize(CodeContext) 
     at Microsoft.Scripting.ScriptCode.Run(CodeContext codeContext, Boolean tryEvaluate) 
     at Microsoft.Scripting.ScriptModule.Execute() 
     at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteFile(String path) 
     at ExEdit.PluggerInner.RunSchemePlugin(String fileName) in D:\VSProjects\ExEdit\Infra.cs:line 35 
     at ExEdit.PluggerInner.RunPlugins() in D:\VSProjects\ExEdit\Infra.cs:line 44 
     at ExEdit.MainForm.MainForm_Load(Object sender, EventArgs e) in D:\VSProjects\ExEdit\MainForm.cs:line 22 
     at System.Windows.Forms.Form.OnLoad(EventArgs e) 
     at System.Windows.Forms.Form.OnCreateControl() 
     at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) 
     at System.Windows.Forms.Control.CreateControl() 
     at System.Windows.Forms.Control.WmShowWindow(Message& m) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ScrollableControl.WndProc(Message& m) 
     at System.Windows.Forms.ContainerControl.WndProc(Message& m) 
     at System.Windows.Forms.Form.WmShowWindow(Message& m) 
     at System.Windows.Forms.Form.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow) 
     at System.Windows.Forms.Control.SetVisibleCore(Boolean value) 
     at System.Windows.Forms.Form.SetVisibleCore(Boolean value) 
     at System.Windows.Forms.Control.set_Visible(Boolean value) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.Run(Form mainForm) 
     at ExEdit.Program.Main() in D:\VSProjects\ExEdit\Program.cs:line 18 
     at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

我的例子Scheme代碼:

(define (test) (+ 1 1)) 

我不知道什麼異常實際上是在談論,因爲它沒有的InnerException。

+0

我對方案一無所知,但是對於DLR,您是否使用ScriptEninge.Execute方法驗證了基本腳本的工作原理?這就是我從IronRuby開始的地方。 – sipwiz

+0

我現在就試試吧......謝謝! :) –

+0

它很奇怪;我可以'engine.Execute(「(+ 1 1)」)沒有例外,但是當我添加幾行來使用WinForms時,它會停止。 –

回答

5

我不認爲我正確實施了ExecuteFile

最簡單的就是抓住load程序。

也是擴展方法,讓生活更輕鬆。

using IronScheme; // for extension methods 
... 
Callable load = "load".Eval<Callable>(); 
... 
load.Call("myfile.ss"); 

我已經更新了異常處理,以提供更好的錯誤信息(我希望!)。

+0

謝謝!我會嘗試的。 –