我在函數中有一個數組對象變量[ref] $InsertColHeadName = @()
,然後調用另一個函數param ([ref] $InsertColHeadName)
。在被調用的函數中,我嘗試引用我在參數([ref] $InsertColHeadName) += expression
中設置的參數。該表達式返回一個字符串。我在該行中放置了一個斷點,並嘗試強制使用單引號的字符串,例如:([ref] $InsertColHeadName) += 'xyz';
。設置通過引用傳遞的數組變量
我試着用google搜索,但似乎找不到合適的解決方案。
我收到以下錯誤:
Method invocation failed because [System.Management.Automation.PSReference`1 [[System.Management.Automation.PSReference`1 [[System.Management.Automation.PSReference`1 [[System.Object[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]] does not contain a method named 'op_Addition'. At line:1 char:1 + ([ref] $InsertColHeadName) += 'DepartmentNo'; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
爲什麼你首先使用'[ref] $ arr = @()'而不是'$ arr = @()'? –
在我在主函數中使用它之前,它不需要參考。由於主函數使用特定的邏輯幾次,它是相同的,有一些驗證邏輯,我認爲將它推到一個函數並調用函數會更好。我試圖引用的變量用於另一個可能需要該變量的函數調用。 –