ArrayList 展开 [英] ArrayList Unrolling

查看:42
本文介绍了ArrayList 展开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Powershell 展开让我发疯.

Powershell unrolling is driving me crazy.

我有以下代码可以从交换收件人处检索电子邮件地址.我使用 ArrayList 是因为当您希望能够从数组中删除项目时,很多人都建议使用它.

I have the following code to retrieve email addresses from an exchange recipient. I'm using the ArrayList because it is suggested by many people when you want the ability to remove items from the array.

$aliases = New-Object System.Collections.ArrayList
$smtpAddresses = (Get-Recipient $this.DN).EmailAddresses | ?{$_.Prefix.ToString() -eq 'smtp' } 
    foreach ($smtpAddress in $smtpAddresses) {
        $aliases.Add($smtpAddress.SmtpAddress)
    }
return $aliases

$aliases 的值在函数末尾是正确的(即将包含 x 个电子邮件地址并且是 ArrayList 类型),但在返回后它变成 System.Object[] 并且有 2x 个条目.有 x 个 Int32 后跟 x 个字符串(即 {0, 1, bob@here, bob@there} ).为什么会发生这种情况以及如何保持我的 ArrayList 完好无损?我使用 ArrayList 有错吗?

The value of $aliases is correct at the end of the function (i.e. will contain x email addresses and is type ArrayList) but after returning it becomes System.Object[] and has 2x entries. There x Int32's followed by x Strings (i.e. {0, 1, bob@here, bob@there} ). Why does this happen and how to I keep my ArrayList intact? Am I wrong for using ArrayList?

出于好奇,PS 展开导致的所有问题/问题,其目的是什么?powershell 的一大好处是您可以直接使用对象而不是它们的文本投影,不幸的是,我永远不知道我在使用哪种对象 - 即使我检查时,它似乎也没有保持其形状更多而不是几行代码.

Out of curiosity, with all the questions/problems resulting from PS unrolling, what is its purpose? The big benefit of powershell is that you work directly with objects instead of their textual projections, unfortunately, I never know what kind of object I'm working with - and even when I check, it doesn't seem to hold its shape for more than a few lines of code.

-- 编辑该函数作为 PSObject 的一部分被调用

-- Edit The function is called as part of a PSObject

$get_aliases = { ... }
$obj | Add-Member -MemberType ScriptProperty -Name Aliases -Value $get_aliases -SecondValue $set_aliases

推荐答案

部分问题在于函数内部如何使用数组.请记住,PowerShell 中的函数实际上不会返回任何内容.它将对象写入管道.因此,返回是多余的,但实际上并没有造成任何问题.使用 Add 函数会导致问题,因为 Add 返回添加值的索引,因此也写入管道.

Part of the problem is how the array is being used inside the function. Remember, a function in PowerShell doesn't actually return anything. It writes objects to the pipeline. Therefore, the return is superfluous, but not actually causing any problems. The use of the Add function is causing the problem because Add returns the index at which the value was added and therefore writes to the pipeline as well.

function get-myarray
{ 
   $al = New-Object System.Collections.ArrayList
   $al.Add( 0 ) 
   $al.Add( 1 )
   $al.Add( 'me@co.com' )
   $al.Add( 'you.co.com' )
   return $al 
}

$array = get-myarray
$array.Count
8     

注意大小是8.需要做的是抑制写入Add函数返回的内容.有几种方法可以做到这一点,但这里是一种:

Note how the size is 8. What needs to be done is to suppress the writing of what is returned by the Add function. There are a few ways to do this but here is one:

function get-myarray
{ 
   $al = New-Object System.Collections.ArrayList
   $al.Add( 0 ) | out-null
   $al.Add( 1 )  | out-null
   $al.Add( 'me@co.com' )  | out-null
   $al.Add( 'you.co.com' )  | out-null
   return $al 
}

$array = get-myarray
$array.Count
4 

如果您想从中删除项目,我不认为使用ArrayList"是错误的.

I don't believe the use of `ArrayList' is a wrong one if you want to remove items from it.

展开而言,这值得一个完整的其他问题,并且已经已经解决.

As far as unrolling goes, this deserves a whole other question and has been already addressed.

这篇关于ArrayList 展开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆