LinqLinker.psm1

<#
    LinqLinker script setup & cmdlets
    Copyright (C) 2026 Joshua 'Joan Metek(illot)' Kidder

    This module is free software: you can redistribute it and/or modify
    it under the terms of the GNU Less General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This module is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this module. If not, see <https://www.gnu.org/licenses/>.
#>


using namespace System.Linq
using namespace System.Reflection
Write-Host (@'
Copyright © 2026 Joshua 'Joan Metek(illot)' Kidder
This module comes with ABSOLUTELY NO WARRANTY; for details see LinqLinker/COPYING
This is free software, and you are welcome to redistribute it under certain conditions;
see LinqLinker/COPYING.LESSER
'@
).Trim()
class ValidLinqMethodGenerator : System.Management.Automation.IValidateSetValuesGenerator {
    [string[]] GetValidValues(){
        [string[]]$values = [Enumerable].GetMethods([BindingFlags]'Public,Static').Name | Get-Unique
        return $values
    }
}
function New-LinqLinker {
    [CmdletBinding()]
    [OutputType([System.Linq.LinqLinker`1])]
    param(
        [Parameter(Mandatory, Position = 0, ValueFromPipeline)]
        [System.Collections.IEnumerable]$IEnumerable
    )
    $GenericTypeArguments = $IEnumerable.GetType().GenericTypeArguments
    if($one = $GenericTypeArguments[0]){
        # It implements the generic Interface so we can use the easy constructor
        ,[System.Linq.LinqLinker]::Link($IEnumerable)
    } else {
        # We gotta go bald...
        [System.Collections.Generic.IEnumerable[psobject]]$Casted = [System.Linq.Enumerable]::Cast[psobject]($IEnumerable)
        ,[System.Linq.LinqLinker]::Link($Casted)
    }
}

Set-Alias -Name 'Add-LinqLink' -Value 'New-LinqLinker'
Set-Alias -Name 'ConvertTo-LinqLinked' -Value 'New-LinqLinker'