Wouter de Kort
Wouter de Kort

Reputation: 39898

PowerShell Invoke returns array instead of int

I have the following snippet of PowerShell:

$property = @{
    Getter = { 80 };
}

$value = $property.Getter.Invoke() 
$value.GetType() # Shows Name = Collection'1 instead of int

I would expect $value.GetType() to return Int32 but instead it returns a collection.

I can't just take element [0] because sometimes the Getter function will return an array.

How can I fix this?

Upvotes: 3

Views: 273

Answers (1)

Fors1k
Fors1k

Reputation: 520

You can strictly declare the type of the return value by this way. For example, the return type will be Double :

cls
$property = @{
    Getter = [Func[Double]]{ 80 }
}

$value = $property.Getter.Invoke() 
$value.GetType().Name
# Double

Upvotes: 5

Related Questions