如何使用与其他DLR语言类似的API在ac#app中托管PowerShell 3.0

Bra*_*ell 8 c# powershell dynamic-language-runtime powershell-3.0

我一直在玩ac#app,主持IronPython,IronRuby和(希望)PowerShell.由于IronPython和IronRuby完全基于DLR构建,因此使用它们的API几乎完全相同.

IronPython.Hosting.Python.CreateEngine()
Run Code Online (Sandbox Code Playgroud)

IronRuby.Ruby.CreateEngine()
Run Code Online (Sandbox Code Playgroud)

两者都创建Microsoft.Scripting.Hosting.ScriptEngine的实例.有没有希望强制PowerShell 3.0创建ScriptEngine?我没有找到很多关于这个主题的内容,除了PowerShell 3.0似乎比以前的版本更多地建立在DLR 上(参见http://huddledmasses.org/powershell-3-finally-on-the-dlr.

您似乎无法将使用以下内容创建的PowerShell引擎强制转换为ScriptEngine.

System.Management.Automation.PowerShell.Create()
Run Code Online (Sandbox Code Playgroud)

我怀疑,如果我真的想通过相同的API处理PowerShell,我需要创建自己的包装PowerShell主机的ScriptEngine.

Jas*_*irk 4

PowerShell V3 确实使用了部分 DLR,但只是 .Net Frameworks V4 中附带的部分。DLR 项目有许多未包含在.Net 中的有用API,例如Microsoft.Scripting.Hosting.ScriptEngine。

如果 PowerShell 想要实现 ScriptEngine api,PowerShell 必须将这些 api 作为 PowerShell 的一部分提供。PowerShell 团队不愿意拥有这些 API。

因此,如果您想实现 ScriptEngine,您的假设是正确的,您需要用自己的代码包装 PowerShell。