【问题标题】:How do I access the object args returned from a .NET event from Powershell如何访问从 Powershell 的 .NET 事件返回的对象 args
【发布时间】:2013-06-09 14:53:20
【问题描述】:

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

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

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

然后是在代码中触发事件的调用

if (ReportProgressEvent != null)
   ReportProgressEvent(update);

如何在 powershell 中读取“更新”对象?

【问题讨论】:

    标签: c# .net powershell


    【解决方案1】:

    您可以在 -Action 脚本块中使用 $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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 2010-09-06
      • 2015-05-01
      • 2021-12-12
      • 2011-01-15
      相关资源
      最近更新 更多