我尝试用c#代码编写一个powershell脚本。脚本代码
$code = @"
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
"@
[Reflection.Assembly]::LoadWithPartialName("System")
[Reflection.Assembly]::LoadWithPartialName("System.Collections.Generic")
[Reflection.Assembly]::LoadWithPartialName("System.IO")
[Reflection.Assembly]::LoadWithPartialName("System.Linq")
[Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq")
[Reflection.Assembly]::LoadWithPartialName("System.Runtime.InteropServices")
[Reflection.Assembly]::LoadWithPartialName("System.Text.RegularExpressions")
$refs = @("System", "System.Collections.Generic", "System.IO", "System.Linq", "System.Xml.Linq", "System.Runtime.InteropServices", "System.Text.RegularExpressions")
Add-Type -ReferencedAssemblies $refs -TypeDefinition $code
当我展示我的脚本时,我得到了错误
GAC Version Location
--- ------- --------
True v4.0.30319 C:WindowsMicrosoft.NetassemblyGAC_MSILSystemv4.0_4.0.0.0__b77a5c561934e089System.dll
True v4.0.30319 C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.IOv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.IO.dll
True v4.0.30319 C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Linqv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.Linq.dll
True v4.0.30319 C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Xml.Linqv4.0_4.0.0.0__b77a5c561934e089System.Xml.Linq.dll
True v4.0.30319 C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Runtime.InteropServicesv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.Runtime.InteropServices.dll
True v4.0.30319 C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Text.RegularExpressionsv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.Text.RegularExpressions.dll
Add-Type : (0) : Metadata file 'System.Collections.Generic.dll' could not be found
(1) : using System;
At line:22 char:1
+ Add-Type -ReferencedAssemblies $refs -TypeDefinition $code
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (error CS0006: M...ld not be found:CompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : Cannot add type. There were compilation errors.
At line:22 char:1
+ Add-Type -ReferencedAssemblies $refs -TypeDefinition $code
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
我怎么能系统。集合。通用装配?
System.Collections.Generic
命名空间不在它自己的DLL中,它包含在mscorlib.dll
中。
请尝试使用System.Core
。