简体   繁体   English

将PowerShell与.NET 3.5运行时/库一起使用

[英]Using PowerShell with .NET 3.5 runtime/libraries

Is it possible to run PowerShell 1.0 (or 2.0 CTP) backed by the 3.5 runtime instead of 2.0? 是否可以运行3.5运行时支持的PowerShell 1.0(或2.0 CTP)而不是2.0?

We're building a .NET 3.5 solution, and I'd still like to use PowerShell as our scripting engine for scheduled tasks, etc. 我们正在构建.NET 3.5解决方案,我仍然希望使用PowerShell作为计划任务的脚本引擎等。

I don't need LINQ syntax or anything, just the 3.5 libraries and runtime. 我不需要LINQ语法或任何东西,只需要3.5库和运行时。

FOLLOWUP: thank you for the reply about dynamically loading assemblies. 关注:感谢您关于动态加载程序集的回复。 But let me clarify my question: is there any way to run PowerShell so that the 3.5 libraries run by default? 但是让我澄清一下我的问题:有没有办法运行PowerShell以便默认运行3.5库? So that if I enter New-Object System.Xml.XmlDocument , for example, I'm actually getting the 3.5 version by default? 因此,如果我输入New-Object System.Xml.XmlDocument ,我实际上是默认获得3.5版本?

Semi-related question: if I do have to dynamically load, say, the System.Xml library from 3.5, will it overlay the existing symbol definitions such that the next time I type New-Object System.Xml.XmlDocument , it will load the 3.5 version? 半相关的问题:如果有动态加载,也就是说, System.Xml 3.5库,将它覆盖现有的符号定义,这样, 一次我键入New-Object System.Xml.XmlDocument它将装载3.5版本?

If you have 3.5 installed on your system, that is what you'll get when you run PowerShell. 如果您的系统上安装了3.5,则运行PowerShell时将获得此功能。

PowerShell is only "requires" 2.0 but 3.0 and 3.5 are compatible and autoforward when installed. PowerShell只是“需要”2.0,但3.0和3.5在安装时是兼容和自动装配的。 In PowerShell V2, we actually look to see what version you have and "light up" certain features (eg PowerShell_ISE and Out-GridView are available when you have 3.51). 在PowerShell V2中,我们实际上会查看您拥有的版本并“点亮”某些功能(例如,当您拥有3.51时,PowerShell_ISE和Out-GridView可用)。

Experiment! 实验! Enjoy! 请享用! Engage! 从事!

Jeffrey Snover [MSFT] Windows Management Partner Architect Jeffrey Snover [MSFT] Windows管理合作伙伴架构师

As long as your get your fully qualified name right, I don't see why this wouldn't work: 只要您获得完全合格的名称,我就不明白为什么这不起作用:

[System.Reflection.Assembly]::Load("System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

I only used Linq as that was the first one to come to mind. 我只使用了Linq,因为这是第一个想到的。 At that point, the ddl is loaded, and you can create/use objects out of that. 此时,ddl已加载,您可以创建/使用该对象。

PowerShell was built against 2.0, so you don't have any options but to have 2.0 present at least. PowerShell是针对2.0构建的,所以你没有任何选择,但至少要有2.0。 But like James says, you can load 3.0 and 3.5 functionality by loading the appropriate assembly. 但是像James说的那样,您可以通过加载适当的程序集来加载3.0和3.5功能。 LINQ is a good example from 3.5, but you can also do WPF (3.0) from PowerShell. LINQ是3.5的一个很好的例子,但你也可以从PowerShell做WPF(3.0)。 Be mindful of STA and MTA for WPF though, as only PowerShell v2 has full support for WPF (full support for thread affinity). 请注意WPF的STA和MTA,因为只有PowerShell v2完全支持WPF(完全支持线程关联)。

3.5 is effectively an ADD-ON for 2.0. 3.5实际上是2.0的ADD-ON。 That is to say, there are no superceded classes in 3.5; 也就是说,3.5中没有超级类; it is not a replacement. 它不是替代品。 The CLR (common language runtime) is still v2.0. CLR(公共语言运行库)仍然是v2.0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM