softshipper
softshipper

Reputation: 34071

Powershell with c# System.Collections.Generic.dll could not be found

I try to write a powershell script with c# code. Script code

$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

When i exucute my script, i've got the error

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

How i can System.Collections.Generic assembly?

Upvotes: 1

Views: 3502

Answers (1)

DavidG
DavidG

Reputation: 118947

The System.Collections.Generic namespace isn't in it's own DLL, it's contained in mscorlib.dll.

Try using System.Core instead.

Upvotes: 2

Related Questions