Skip to content

Commit 6b05f7e

Browse files
author
James Brundage
committed
New-PipeScript: Allowing -Parameter to be supplied via reflection (Fixes #171)
1 parent 4c9575a commit 6b05f7e

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

New-PipeScript.ps1.ps1

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@
1717
# * As a ```[ScriptBlock]``` containing only parameters.
1818
[Parameter(ValueFromPipelineByPropertyName)]
1919
[ValidateScriptBlock(ParameterOnly)]
20-
[ValidateTypes(TypeName={[Collections.IDictionary], [string],[Object[]], [Scriptblock]})]
20+
[ValidateTypes(TypeName={
21+
[Collections.IDictionary],
22+
[string],
23+
[Object[]],
24+
[Scriptblock],
25+
[Reflection.PropertyInfo],
26+
[Reflection.PropertyInfo[]],
27+
[Reflection.ParameterInfo],
28+
[Reflection.ParameterInfo[]],
29+
[Reflection.MethodInfo],
30+
[Reflection.MethodInfo[]]
31+
})]
2132
$Parameter,
2233

2334
# The dynamic parameter block.
@@ -147,6 +158,43 @@
147158
$parameter
148159
}
149160
}
161+
elseif ($parameter -is [Reflection.PropertyInfo] -or
162+
$parameter -as [Reflection.PropertyInfo[]] -or
163+
$parameter -is [Reflection.ParameterInfo] -or
164+
$parameter -as [Reflection.ParameterInfo[]] -or
165+
$parameter -is [Reflection.MethodInfo] -or
166+
$parameter -as [Reflection.MethodInfo[]]
167+
) {
168+
if ($parameter -is [Reflection.MethodInfo] -or
169+
$parameter -as [Reflection.MethodInfo[]]) {
170+
$parameter = @(foreach ($methodInfo in $parameter) {
171+
$methodInfo.GetParameters()
172+
})
173+
}
174+
175+
foreach ($prop in $Parameter) {
176+
if ($prop -is [Reflection.PropertyInfo] -and -not $prop.CanWrite) { continue }
177+
$paramType =
178+
if ($prop.ParameterType) {
179+
$prop.ParameterType
180+
} elseif ($prop.PropertyType) {
181+
$prop.PropertyType
182+
} else {
183+
[PSObject]
184+
}
185+
$ParametersToCreate[$prop.Name] =
186+
@(
187+
$parameterAttribute = "[Parameter(ValueFromPipelineByPropertyName)]"
188+
$parameterAttribute
189+
if ($paramType -eq [boolean]) {
190+
"[switch]"
191+
} else {
192+
"[$($paramType -replace '^System\.')]"
193+
}
194+
'$' + $prop.Name
195+
) -ne ''
196+
}
197+
}
150198
}
151199

152200
# If there is header content,

0 commit comments

Comments
 (0)