我的异常发生在这个代码块的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
}
}
}
包含IComparable和IEquatable方法用于调试目的。我收到的例外是:
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
我在这里错过了什么,因为代码看起来很好,直到我昨天最后一次编辑。我花了几个小时试图找出问题所在,但最终我的搜索结果是干的。
1条答案
按热度按时间fnatzsnv1#
您在
.ctor
中有一些语法错误。在PowerShell类构造函数中没有param { ... }
语法。然后,使用Watched
,sFilter
和Options
,就好像它们是命令,如果你示例化的类没有.ctor
参数,它们将结束抛出CommandNotFoundException。要修复该错误,请将
.ctor
定义从以下位置更改:To(注意删除了
param { ... }
并向.ctor
添加了参数):然后你可以示例化
FSEventData
而不会出现问题: