我尝试用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:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll
True v4.0.30319 C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.IO\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.IO.dll
True v4.0.30319 C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Linq\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Linq.dll
True v4.0.30319 C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll
True v4.0.30319 C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.InteropServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.InteropServices.dll
True v4.0.30319 C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Text.RegularExpressions\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.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程序集?
答案 0 :(得分:2)
System.Collections.Generic
命名空间不在其自己的DLL中,它包含在mscorlib.dll
中。
请尝试使用System.Core
。