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

前端 未结 4 1446
没有蜡笔的小新
没有蜡笔的小新 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:26

    PowerShell 5.0 added support for classes and hence methods where the return type can be specified. To control the type returned, use a class with a static method:

    class ListManager {
        static [System.Collections.Generic.List[int]] Create() {
            [System.Collections.Generic.List[int]] $list =
                [System.Collections.Generic.List[int]]::new()
    
            $list.Add(3)
    
            return $list
         }
    }
    
    [ListManager]::Create().GetType()
    

提交回复
热议问题