如何从Powershell访问. net事件返回的对象参数?

本文关键字:返回 对象 参数 事件 net Powershell 访问 | 更新日期: 2023-09-27 18:18:24

好的,所以我对powershell很陌生,我用它来调用我创建的。net dll,到目前为止,直到我尝试使用Register-ObjectEvent cmdlet。我可以将fine附加到我创建的自定义.NET事件,但似乎无法访问从事件传递的对象!

下面是我使用的委托和事件声明

delegate void ReportResult(ProgressUpdate update);
event Delegates.ReportResult ReportProgressEvent

然后是在代码

中触发事件的调用
if (ReportProgressEvent != null)
   ReportProgressEvent(update);

如何在powershell中读取'update'对象?

如何从Powershell访问. net事件返回的对象参数?

您可以在-Action scriptblock中使用$event来访问事件。例如,

$watcher = New-Object System.IO.FileSystemWatcher -Property @{Path = 'C:'Temp' }
$action = { Write-Host (Split-Path -Path $event.sourceEventArgs.FullPath) }
Register-ObjectEvent -InputObject $watcher -EventName Created -SourceIdentifier FileCreated -Action $action