gpt4 book ai didi

powershell - 为什么对伪造数组属性的逻辑测试会返回 true?

转载 作者:行者123 更新时间:2023-12-03 16:46:53 25 4
gpt4 key购买 nike

我最近犯了一个错误,我有一个对象数组,我不小心对数组的属性进行了 bool 计算,但我的意思是对该数组中的单个对象进行计算.我只是在测试期间才发现我的错误,因为当我期望 $FALSE 时,我得到了一个 $TRUE 值。我创建了一个小脚本来说明情况。我不明白的是为什么这个不存在的数组属性总是评估为真?

# Set-StrictMode -Version Latest; # Catch dumb programmer mistakes.
$PSVersionTable.PSVersion
Write-Output "`n"

$coArray = @()
foreach ($id in 0,1)
{
$cObj = New-Object PSobject
$cObj | Add-Member -type NoteProperty -name ID -value $id
$cObj | Add-Member -type NoteProperty -name Bool -value $false
$coArray += $cObj
}

foreach ($cObj in $coArray)
{
# What I meant to do...

if ($cObj.Bool) { Write-Output 'ON OBJECT: Bool is true' }
else { Write-Output 'ON OBJECT: Bool is false' }

# What I actually did...

if ($coArray.Bool) { Write-Output 'ON ARRAY: Bool is true' }
else { Write-Output 'ON ARRAY: Bool is false' }

# What if there is no such object property?

if ($coArray.Bogus) { Write-Output 'ON ARRAY: Bogus is true' }
else { Write-Output 'ON ARRAY: Bogus is false' }
}

这是输出:

Major  Minor  Build  Revision
----- ----- ----- --------
5 1 14393 2339


ON OBJECT: Bool is false
ON ARRAY: Bool is true
ON ARRAY: Bogus is true
ON OBJECT: Bool is false
ON ARRAY: Bool is true
ON ARRAY: Bogus is true

起初我认为它为放错位置的现有对象属性返回 true,因为它确认数组中至少有一个对象具有该属性。但是,如果您关闭 strict 并引用一个完全虚假的属性,如上所示,它仍会返回 true。

最佳答案

嗯……这就是我的想法。我认为这是 Powershell 现在展开数组对象属性的方式的影响。 Powershell 绝对是真实的。

第一个测试 if ($cObj.Bool) 确实按照预期的方式工作。第二个测试 if ($coArray.Bool) 导致 powershell 创建一个新数组,该数组仅包含每个对象的该属性的内容。这可以通过以下方式显示:

C:\Users\Rob> $coArray.bool
False
False

所以在真实的 Powershell 世界中,这是确实存在的东西,所以它是真的。

第三个测试 if ($coArray.Bogus) 几乎做同样的事情。这可以通过以下方式显示:

Get-Member -InputObject $coArray.bogus
TypeName: System.Object[]

Name MemberType Definition
---- ---------- ----------
Count AliasProperty Count = Length
Add Method int IList.Add(System.Object value)
Address Method System.Object&, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a...
Clear Method void IList.Clear()
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
CompareTo Method int IStructuralComparable.CompareTo(System.Object other, System.Collections.ICo...
Contains Method bool IList.Contains(System.Object value)
CopyTo Method void CopyTo(array array, int index), void CopyTo(array array, long index), void...
Equals Method bool Equals(System.Object obj), bool IStructuralEquatable.Equals(System.Object ...
Get Method System.Object Get(int )
GetEnumerator Method System.Collections.IEnumerator GetEnumerator(), System.Collections.IEnumerator ...
GetHashCode Method int GetHashCode(), int IStructuralEquatable.GetHashCode(System.Collections.IEqu...
GetLength Method int GetLength(int dimension)
GetLongLength Method long GetLongLength(int dimension)
GetLowerBound Method int GetLowerBound(int dimension)
GetType Method type GetType()
GetUpperBound Method int GetUpperBound(int dimension)
GetValue Method System.Object GetValue(Params int[] indices), System.Object GetValue(int index)...
IndexOf Method int IList.IndexOf(System.Object value)
Initialize Method void Initialize()
Insert Method void IList.Insert(int index, System.Object value)
Remove Method void IList.Remove(System.Object value)
RemoveAt Method void IList.RemoveAt(int index)
Set Method void Set(int , System.Object )
SetValue Method void SetValue(System.Object value, int index), void SetValue(System.Object valu...
ToString Method string ToString()
Item ParameterizedProperty System.Object IList.Item(int index) {get;set;}
IsFixedSize Property bool IsFixedSize {get;}
IsReadOnly Property bool IsReadOnly {get;}
IsSynchronized Property bool IsSynchronized {get;}
Length Property int Length {get;}
LongLength Property long LongLength {get;}
Rank Property int Rank {get;}
SyncRoot Property System.Object SyncRoot {get;}

正如所指出的,仅对数组中的一个对象进行操作会导致所有测试都为假。这是因为数组中只有一项意味着它只有一个属性可以展开。 Powershell 将其展开为单例对象而不是值数组。因此,如果没有具有该名称的属性,则单例为 null。看这里:

C:\Users\Rob> $b = @((New-Object PSCustomObject -Property @{'notBogus'='foo'}))
C:\Users\Rob> $b

notBogus
--------
foo

C:\Users\Rob> gm -in $b.Notbogus


TypeName: System.String

Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB), int IComparab...
Contains Method bool Contains(string value)
CopyTo Method void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int co...
EndsWith Method bool EndsWith(string value), bool EndsWith(string value, System.StringCompari...
.
.
.

C:\Users\Rob> $b.bogus -eq $null
True

如您所见,$b.NotBogus 只是一个单一值,因此它可以进行适当的测试。

仍然不确定@tessellatingHeckler 提出的反例。我会继续寻找。

关于powershell - 为什么对伪造数组属性的逻辑测试会返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51254988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com