我试图在远程Powershell 2.0会话中使用[Reflection.Assembly] :: LoadFrom加载.Net 4.0程序集.由于我对powershell.exe.config文件所做的更改,它在本地工作,但在远程会话中失败并显示"此程序集由运行时更新...构建"异常.
涉及的两台机器都有.Net 2.0和4.0,并且对x86和x64位powershell可执行文件进行了powershell.exe.config更改.我也尝试更改服务器powershell注册表项:HKLM:\ Software\Microsoft\Powershell\1\PowerShellEngine\RuntimeVersion HKLM:\ Software\Wow6432Node\Microsoft\Powershell\1\PowerShellEngine\RuntimeVersion
我一定错过了什么,但我不知道它是什么.
编辑: 以下是我正在执行的代码的示例.
PS C:\>Enter-PSSession -ComputerName server1
[server1]: PS C:\stuff> dir *.dll | foreach { [Reflection.Assembly]::LoadFrom( $_.FullName ) }
Run Code Online (Sandbox Code Playgroud) 我正在与一组使用Powershell 2.0的自定义.Net 2.0程序集进行交互,以自动执行当前任务.我加载所有必需的dll并在dll中调用DAL方法来检索System.Data.SqlDataReader.当我将SqlDataReader传递到同一个自定义程序集中的构造函数时,我收到"在读取器关闭时无效尝试调用HasRows".例外.
代码示例:
dir D:\stuff\*.dll | foreach { [Reflection.Assembly]::LoadFrom($_.FullName) } | out-null
[CustomAssembly.DataConfig]::ConnectionString = "Valid Connection String"
$reader=[CustomAssembly.DAL.Thing]::Get(123)
new-object CustomAssembly.BusinessObjects.Thing($reader)
Run Code Online (Sandbox Code Playgroud)
在我调用Thing构造函数之前,$ reader是打开的,并且有数据.
我必须遗漏一些东西,但我不确定它是什么.
编辑1:
$ reader在传递给函数,powershell或程序集中的任何时候似乎都被读取和关闭.有办法防止这种情况吗?
编辑2:
Powershell自动展开再次打击
如何阻止PowerShell解压缩Enumerable对象?
PowerShell函数中的奇怪行为返回DataSet/DataTable
以下修改的代码示例通过将结果包装在单个元素数组中来工作,因此自动展开不会影响SqlDataReader.请注意"$ reader ="语句后面的单个逗号.那不是拼写错误.
dir D:\stuff\*.dll | foreach { [Reflection.Assembly]::LoadFrom($_.FullName) } | out-null
[CustomAssembly.DataConfig]::ConnectionString = "Valid Connection String"
$reader=,[CustomAssembly.DAL.Thing]::Get(123)
new-object CustomAssembly.BusinessObjects.Thing($reader)
Run Code Online (Sandbox Code Playgroud) 我正在尝试用SICP学习函数式编程.我想用Clojure.
Clojure是Lisp的一种方言,但我对Lisp非常不熟悉.此代码段不清晰且不可读.如何使用Lisp方言编写更高效的代码?
如何从其他函数传递多个参数函数?
(defn greater [x y z]
(if (and (>= x y) (>= x z))
(if (>= y z)
[x,y]
[x,z])
(if (and (>= y x) (>= y z))
(if (>= x z)
[y,x]
[y,z])
(if (and (>= z x) (>= z y))
(if (>= y x)
[z,y]
[z,x])))))
(defn sum-of-squares [x y]
(+ (* x x) (* y y)))
(defn -main
[& args]
(def greats (greater 2 3 4))
(def sum (sum-of-squares greats)))
Run Code Online (Sandbox Code Playgroud) 如果我在线程宏中使用匿名函数之前定义了它:
(def even #(map even? %))
(-> [1 2 3] even)
Run Code Online (Sandbox Code Playgroud)
我得到正确的结果:
(false true false)
Run Code Online (Sandbox Code Playgroud)
但是,如果我评估以下代码:
(-> [1 2 3] #(map even? %))
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
clojure.lang.Compiler$CompilerException: java.lang.IllegalArgumentException: fn params must be Symbols
Run Code Online (Sandbox Code Playgroud)
为什么我不能在线程宏中使用匿名函数?