I’ve been using C# much more often recently for two main reasons:
1) I need to squeeze every bit of speed out of my code (big data sets); and
2) I need to use C# to fill-in some feature gaps in PowerShell Core
My workflow generally involves using Visual Studio to write an initial draft Class Library, build the solution, test using the Add-Type cmdlet in Windows PowerShell and PowerShell Core, adjust code as necessary, rinse and repeat. Ultimately, my goal is to get my C# working with the Add-Type cmdlet, and bouncing back and forth between Visual Studio and a PowerShell console was starting to get annoying. So, I decided to try and come up with a way to do everything in Windows PowerShell (and PowerShell Core) for more rapid testing.
Link to Module:
https://github.com/pldmgg/misc-powershell/tree/master/MyModules/Load-Assemblies
The Load-Assemblies Module (and specifically, the Load-Assemblies function therein) can take an array of strings that represent assembly names (like ‘System.Collections’, etc), and:
-
Figure out if the referenced assembly is already available in the established Windows Global Assembly Cache (GAC) or Linux equivalent path. If it is NOT available, it will attempt to download the needed assembly from the NuGet Gallery and install it to the GAC.
-
Figure out the appropriate “using” statement to use at the beginning of the C# code
-
Output a PS Custom Object with two properties to be used in Add-Type’s -ReferencedAssemblies and -TypeDefinition parameters
IMPORTANT NOTE: This module DOES load the appropriate version of the named assembly (given the $PSVersionTable.PSEdition and/or $PSVersionTable.CLRVersion), but it does NOT figure out ALL of the assemblies you need to run your C# code. You need to know all of the needed assemblies for your C# code upfront and provide that to the Load-Assemblies function in an array of strings.
EXAMPLE:
PS C:Userstestadmin> [System.Collections.ArrayList]$AssembliesToCheckFor = @( "Microsoft.CSharp", "Newtonsoft.Json", "Newtonsoft.Json.Linq", "Newtonsoft.Json.Schema", "System.IO", "System.Runtime", "System.Runtime.Serialization.Primitives", "System.Dynamic.Runtime", "System.Collections", "System.Collections.Generic", "System.Linq" ) PS C:Userstestadmin> Import-Module Load-Assemblies PS C:Userstestadmin> $AddTypeParamsPrep = Load-Assemblies -AssemblyNames $AssembliesToCheckFor PS C:Userstestadmin> $AddTypeParamsPrep | fl * ReferencedAssemblies : {Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, Newtonsoft.Json.Schema, Version=2.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a...} TypeDefinitionUsingStatements : using Microsoft.CSharp; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Schema; using System.IO; using System.Runtime; using System.Runtime.Serialization; using System.Dynamic; using System.Collections; using System.Collections.Generic; using System.Linq;
The $AddTypeParamsPrep.ReferencedAssemblies property is ready to be used in Add-Type’s -ReferencedAssemblies parameter. Add the $AddTypeParamsPrep.TypeDefinitionUsingStatements property to the top of your double-quoted HereDoc string containing your C# code, and you’re ready to use the Add-Type cmdlet to test your C#.
EXAMPLE (unrelated to the values used in the earlier example)
$Source = @" $($AddTypeParamsPrep.TypeDefinitionUsingStatements) namespace PaulD.IPTools { public static class TCPConnections { public static void ShowActiveTcpConnections() { Console.WriteLine("Active TCP Connections"); IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); TcpConnectionInformation[] connections = properties.GetActiveTcpConnections(); foreach (TcpConnectionInformation c in connections) { Console.WriteLine("{0} <==> {1}", c.LocalEndPoint.ToString(), c.RemoteEndPoint.ToString()); } } } } "@ Add-Type -ReferencedAssemblies $AddTypeParamsPrep.ReferencedAssemblies -TypeDefinition $Source [PaulD.IPTools.TCPConnections]::ShowActiveTcpConnections()
Hope this can help some folks, especially when filling in feature gaps for PowerShell Core!
submitted by /u/fourierswager
[link] [comments]
The post New Module: Load-Assemblies – Prepare values for the Add-Type cmdlet’s -ReferencedAssemblies and -TypeDefinition parameters. appeared first on How to Code .NET.