powershell - Storing datatypes and embedding them in other datatypes -
i can store data type in variable this
$type = [int]
and use this:
$type.gettype().name
but, how can embed in declaration? e.g.
[func[$type]]
* update *
so invoke-expression
trick (thanks mike z). trying create lambda expression. how can now:
$exp = [system.linq.expressions.expression] $ir = [neo4jclient.cypher.icypherresultitem] invoke-expression "`$functype = [func[$ir]]" $ret = $exp::lambda($functype, ...)
but @petseral , @jan interesting alternatives
this not appear possible directly, @ least according powershell 3.0 specification.
the [type]
syntax called type-literal spec , definition not included parts can expressions. composed of type-names composed of type-characters there nothing dynamic them.
reading through spec, noticed works:
$type = [int] $try = read-host $type::"$(if ($try) { 'try' } else { '' })parse"
now might wonder why $type::$variable
allowed. because ::
operator who's left hand side expression must evaluate type. right hand side member-name allows simple names, string literals, , use of subexpression operator.
however powershell extremely resilient , can dynamicly via invoke-expression
. let's want declare variable generic delegate based on type know @ runtime:
$type = [int] # come somewhere else entirely invoke-expression "`$f = [func[$type]]{ return 1 }"
now $f
has delegate. need test out if $type complex nested or generic type should work basic types. tested [int]
, [system.collections.generic.list[int]]
worked fine both.
Comments
Post a Comment