PoshCode Archive  Artifact [1214552686]

Artifact 1214552686a3cb4b27dcd3de5156b24850cd145d798b1c435b43d2a06a7742e3:

  • File New-Struct.ps1 — part of check-in [fe9bcd4e17] at 2018-06-10 13:02:38 on branch trunk — A code-generating and emitting magic function for creating type-safe struct classes for use in PowerShell! (user: Joel Bennett size: 2258)

# encoding: ascii
# api: powershell
# title: New-Struct
# description: A code-generating and emitting magic function for creating type-safe struct classes for use in PowerShell!
# version: 0.1
# type: class
# author: Joel Bennett
# license: CC0
# function: New-Struct
# x-poshcode-id: 190
# x-archived: 2016-03-03T14:51:50
# x-published: 2009-05-02T07:46:00
#
# rev 1 – new join function
#
## New-Struct
##   Creates a Struct class and emits it into memory
##   The Struct includes a constructor which takes the parameters in order...
## 
## Usage:
##   # Assuming you have a csv file with no header and columns: artist,name,length
##   New-Struct Song @{
##     Artist=[string];
##     Name=[string];
##     Length=[TimeSpan];
##   }
##   $songs = gc C:\Scripts\songlist.csv | % { new-object Song @($_ -split ",") }
##
function New-Struct {
	param([string]$Name,[HashTable]$Properties)
	switch($Properties.Keys){{$_ -isnot [String]}{throw "Invalid Syntax"}}
	switch($Properties.Values){{$_ -isnot [type]}{throw "Invalid Syntax"}}

	# CODE GENERATION MAGIKS!
	$code = @"
using System;
public struct $Name {

  $($Properties.Keys | % { "  public {0} {1};`n" -f $Properties[$_],($_.ToUpper()[0] + $_.SubString(1)) })
  public $Name ($( [String]::join(',',($Properties.Keys | % { "{0} {1}" -f $Properties[$_],($_.ToLower()) })) )) {
    $($Properties.Keys | % { "    {0} = {1};`n" -f ($_.ToUpper()[0] + $_.SubString(1)),($_.ToLower()) })
  }
}
"@

	## Obtains an ICodeCompiler from a CodeDomProvider class.
	$provider = New-Object Microsoft.CSharp.CSharpCodeProvider
	## Get the location for System.Management.Automation DLL
	$dllName = [PsObject].Assembly.Location
	## Configure the compiler parameters
	$compilerParameters = New-Object System.CodeDom.Compiler.CompilerParameters
	$assemblies = @("System.dll", $dllName)

	$compilerParameters.ReferencedAssemblies.AddRange($assemblies)
	$compilerParameters.IncludeDebugInformation = $true
	$compilerParameters.GenerateInMemory = $true

	$compilerResults = $provider.CompileAssemblyFromSource($compilerParameters, $code)
	if($compilerResults.Errors.Count -gt 0) {
	  $compilerResults.Errors | % { Write-Error $_.Line + ":`t" + $_.ErrorText }
	}
}