比较对象

如何通过-eq.NET 对象创建可比较的对象。例如:

class test{
    $a
    $b
    test($a,$b){
        $this.a=$a
        $this.b=$b
    }
}
$obj = [test]::new(4, 5) 
$obj -eq [test]::new(4, 5)
# False

$pt = [System.Drawing.Point]::new(4, 5)
$pt -eq [System.Drawing.Point]::new(4, 5)
# True

回答

根据文档:

要创建可比较的类,您需要System.IEquatable<T>在类中实现。

class MyFileInfoSet : System.IEquatable[Object] {
    [String]$File
    [Int64]$Size

    [bool] Equals([Object] $obj) {
        return ($this.File -eq $obj.File) -and ($this.Size -eq $obj.Size)
    }
}
$a = [MyFileInfoSet]@{File = "C:Windowsexplorer.exe"; Size = 4651032}
$b = [MyFileInfoSet]@{File = "C:Windowsexplorer.exe"; Size = 4651032}
$a -eq $b


以上是比较对象的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>