How to use add-type with -path as well as -language csharpversion3?

I am using add-type in Powershell to dynamically compile in some C # classes that I want to use. Works great except 2.0.

I just opened the -language csharpversion3 , but it does not work with -path . How can I get around this?

[Edit: bit about ReadAllText was deleted - I was wrong.]

+4
source share
2 answers

Here's a workaround: read the file as text.

 $script = [io.file]::readalltext($scriptpath) add-type $script -lang csharpversion3 

I could also insert the rest and make this answer useful in some way. I have a debug flag that allows me to generate a DLL so that I can break in with the debugger more easily, inspect it with Reflector, etc.

 $cp = new-object codedom.compiler.compilerparameters $cp.ReferencedAssemblies.Add('system.dll') > $null $cp.ReferencedAssemblies.Add('system.core.dll') > $null # optionally turn on debugging support if ($debugscript) { # delete old unused crap while we're at it dir "$($env:temp)\-*.dll" |%{ del $_ -ea silentlycontinue if ($?) { del $_.fullname.replace('.dll', '.pdb') -ea silentlycontinue } } $cp.TreatWarningsAsErrors = $true $cp.IncludeDebugInformation = $true $cp.OutputAssembly = $env:temp + '\-' + [diagnostics.process]::getcurrentprocess().id + '.dll' } $script = [io.file]::readalltext($scriptpath) add-type $script -lang csharpversion3 -compilerparam $cp 

This adds some additional features if the $ debugscript parameter is set to true:

  • Compile with warnings as errors
  • PDB creation
  • Use the specific DLL / PDB name (in the temp folder) associated with the process identifier so that each session gets its own. This is useful for repeating changes to .cs.
  • Removes old dll and pdb, also good at iteration.
+4
source

I don’t know if this is exactly what you need, but you can enable .NET 4 support in powershell. Remember that it uses .NET 2 by default. To do this, you need to add powershell.exe.config and this content in $ PSHome XML with this name:

 <?xml version="1.0"?> <configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0.30319"/> <supportedRuntime version="v2.0.50727"/> </startup> </configuration> 

After that, any code oriented to a large .net version will work, for example:

 Slytherin>> gc new.cs using System.IO; public static class testeo { public static string joinP(string[] arr) { return Path.Combine(arr); } } Slytherin>> $arr = "uno", "dos", "tres", "cuatro", "cinco" Slytherin>> Add-Type -Path .\new.cs Slytherin>> [testeo]::joinP($arr) uno\dos\tres\cuatro\cinco 

This method uses Path.Combine with N arguments that are defined in .NET4. In .NET2, Combine can handle up to two arguments. I am testing another example that uses anonymous delegates, one of the characteristics of C # 3:

 Slytherin>> $sharp = [IO.file]::readalltext((resolve-path new.cs)) Slytherin>> add-type -typedef $sharp -Language CsharpVersion3 Slytherin>> gc .\new.cs using System.IO; using System; using System.Linq; namespace grantest { public static class testeo { public static void joinP(string[] arr) { arr.ToList<string>().ForEach(x=>Console.WriteLine(x)); } } } Slytherin>> [grantest.testeo]::joinP($arr) uno dos tres cuatro cinco Slytherin>> 

Last example, if you try the following code, it will not work

 Slytherin>> $sharp = [IO.file]::readalltext((resolve-path new.cs)) Slytherin>> $sharp using System.IO; using System; using System.Linq; namespace grantest { public static class testeo4 { public static string joinP(string[] arr) { arr.ToList<string>().ForEach(x=>Console.WriteLine(x)); return Path.Combine(arr); } } } Slytherin>> Add-Type -TypeDefinition $sharp Add-Type : c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(3) : The type or namesp ace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?) c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(2) : using System; c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(3) : >>> using System.Linq; c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(4) : At line:1 char:9 + Add-Type <<<< -TypeDefinition $sharp + CategoryInfo : InvalidData: (c:\Users\voodoo...bly reference?):Compile rError) [Add-Type], Exception + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddType Command 

try to specify the language parameter

 Slytherin>> add-type -TypeDefinition $sharp -Language CSharpVersion3 Add-Type : c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(13) : No overload for m ethod 'Combine' takes '1' arguments c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(12) : arr.ToList<string>() .ForEach(x=>Console.WriteLine(x)); c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(13) : >>> return Path.Comb ine(arr); c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(14) : } At line:1 char:9 + add-type <<<< -TypeDefinition $sharp -Language CSharpVersion3 + CategoryInfo : InvalidData: (c:\Users\voodoo...s '1' arguments:Compile rError) [Add-Type], Exception + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddType Command 

This is because if you use the .net4 method and, apparently, if you say that the version 3 language automatically uses the .net 3 libraries. To do this, you need to add System.Core as a reference assembly and forget about language parameter (Csharpversion4 does not have an enumeration of possible values), so it will use version 4 because we included it earlier.

 Slytherin>> Add-Type -TypeDefinition $sharp -ReferencedAssemblies System.core Slytherin>> [grantest.testeo4]::joinP($arr) uno dos tres cuatro cinco uno\dos\tres\cuatro\cinco 

Good luck and happy coding.

+4
source

Source: https://habr.com/ru/post/1345746/


All Articles