You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell调用类构造函数报错:找不到匹配的3参数重载

PowerShell类构造函数重载找不到问题排查

异常代码位置

异常发生在Watch方法的foreach循环中:

[void] Watch([System.IO.DirectoryInfo]$Directory, [string[]]$sFilters, [FSWatcherOptions]$Options) {
    foreach($sFilter in $sFilters) {
        $FSEventData = [FSEventData]::new($Directory, $sFilter, $Options)
        ...
    }
}

相关依赖代码

同一脚本中异常位置上方约80行的类与枚举定义:

enum FSWatcherOptions {
    Recurse = 1
}
class FSEventData : IComparable, IEquatable[Object] {
    [System.IO.DirectoryInfo]$Watched
    [string]$sFilter
    [FSWatcherOptions]$Options

    FSEventData()  {
        param {
            [System.IO.DirectoryInfo]$Watched,
            [string]$sFilter,
            [FSWatcherOptions]$Options
        }
        $this.Watched = Watched
        $this.sFilter = sFilter
        $this.Options = Options
    }
    [string] ToString() {
        return "Watched:$($this.Watched.FullName)Filter:$($this.sFilter)Options:$($this.Options.ToString())"
    }
    [FSEventData] Clone() {
        return [FSEventData]::new($this.Watched, $this.sFilter, $this.Options)
    }
    hidden [int] CompareWatched([FSEventData]$that) {
        [int] $out = 0
        if ($this.Watched -gt $that.Watched) {$out =  1}
        if ($this.Watched -lt $that.Watched) {$out = -1}
        return $out
    }
    hidden [int] CompareFilter ([FSEventData]$that) {
        [int] $out = 0
        if ($this.sFilter -gt $that.sFiler) {$out =  1}
        if ($this.sFilter -lt $that.sFiler) {$out = -1}
        return $out
    }
    hidden [int] CompareOptions([FSEventData]$that) {
        [int] $out = 0
        if ($this.Options -gt $that.Options) {$out =  1}
        if ($this.Options -lt $that.Options) {$out = -1}
        return $out
    }
    [int] CompareTo($that) {
        If (-Not($that -is [FSEventData])) {
            Throw "Not Comparable!!"
        }
        [int]$out = 0
        $out = $this.CompareWatched([FSEventData]$that)
        if ($out -eq 0) {$out = $this.CompareFilter}
        if ($out -eq 0) {$out = $this.CompareOptions}
        return $out
    }
    [bool] Equals  ($that) {
        If (-Not($that -is [FSEventData])) {
            Throw "Not Equatable!!"
        }
        return {
            $this.Watched -eq $that.Watched -and
            $this.sFilter -eq $that.sFilter -and
            $this.Options -eq $that.Options
        }
    }
}

异常信息

Cannot find an overload for "new" and the argument count: "3".
At X:\FFFF\FileSystemEventProcessor.ps1:95 char:13
+             $FSEventData = [FSEventData]::new($Directory, $filter, $O ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

问题根源与修复

核心问题:构造函数定义错误

PowerShell类的构造函数不能使用param块,正确写法是将参数直接声明在构造函数名称后的括号内。你当前的代码定义了一个无参构造函数FSEventData(),内部的param块不会被识别为构造函数的参数,导致类实际上没有接收3个参数的构造函数,调用[FSEventData]::new(3个参数)时自然找不到对应重载。

修正后的构造函数

FSEventData([System.IO.DirectoryInfo]$Watched, [string]$sFilter, [FSWatcherOptions]$Options) {
    $this.Watched = Watched
    $this.sFilter = sFilter
    $this.Options = Options
}

附带的其他问题修复

除了构造函数,代码中还有几处小问题需要修正,否则后续运行仍会报错:

  1. CompareFilter方法拼写错误:将$that.sFiler改为$that.sFilter
  2. CompareTo方法调用错误:调用CompareFilter和CompareOptions时需要传入$that参数,即$out = $this.CompareFilter($that)、$out = $this.CompareOptions($that)
  3. Equals方法返回值错误:当前返回的是脚本块(大括号包裹),应直接返回布尔表达式,去掉外层大括号

修正后的完整FSEventData类关键部分:

class FSEventData : IComparable, IEquatable[Object] {
    [System.IO.DirectoryInfo]$Watched
    [string]$sFilter
    [FSWatcherOptions]$Options

    FSEventData([System.IO.DirectoryInfo]$Watched, [string]$sFilter, [FSWatcherOptions]$Options) {
        $this.Watched = Watched
        $this.sFilter = sFilter
        $this.Options = Options
    }

    hidden [int] CompareFilter ([FSEventData]$that) {
        [int] $out = 0
        if ($this.sFilter -gt $that.sFilter) {$out =  1}
        if ($this.sFilter -lt $that.sFilter) {$out = -1}
        return $out
    }

    [int] CompareTo($that) {
        If (-Not($that -is [FSEventData])) {
            Throw "Not Comparable!!"
        }
        [int]$out = 0
        $out = $this.CompareWatched([FSEventData]$that)
        if ($out -eq 0) {$out = $this.CompareFilter($that)}
        if ($out -eq 0) {$out = $this.CompareOptions($that)}
        return $out
    }

    [bool] Equals  ($that) {
        If (-Not($that -is [FSEventData])) {
            Throw "Not Equatable!!"
        }
        return $this.Watched -eq $that.Watched -and
               $this.sFilter -eq $that.sFilter -and
               $this.Options -eq $that.Options
    }

    // 其他方法保持不变
}

内容的提问来源于stack exchange,提问作者Jaaz Cole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 07:00:18