PowerShell converts values returned from function. How to avoid this?

前端 未结 4 1338
没有蜡笔的小新
没有蜡笔的小新 2021-01-13 06:59

I am trying to return List< T> from PowerShell function, but get one of:

  1. null - for empty list
  2. System.Int32 - for list with one element
  3. Sy
4条回答
  •  失恋的感觉
    2021-01-13 07:10

    If you need to return a list of ints, use jachymko's solution.

    Otherwise, if you do not particularly care whether what you get is a list or array, but just want to iterate through result, you can wrap result in @() while enumerating, e.g.

    $fooList = CreateClrList
    foreach ($foo in @($fooList))
    {
        ...
    }
    

    That would cause @($fooList) to be of array type - either an empty array, array with one element or array with multiple elements.

提交回复
热议问题