在 PowerShell 中将 .NET 方法作为委托回调传递给 .NET 对象的语法是什么.
What is the syntax to pass .NET method as a delegate callback to a .NET object in PowerShell.
例如:
C#:
public class Class1
{
public static void MyMethod(Action<object> obj)
{
obj("Hey!");
}
}
public class Class2
{
public static void Callback(object obj)
{
Console.Writeline(obj.ToString());
}
}
PowerShell:
PowerShell:
[Class1]::MyMethod([Class2]::Callback)
这不起作用.
通过 Adam 和 Oisin 聊天的工作代码.
Working code via Adam's and Oisin's chat.
Add-Type -Language CSharpVersion3 -TypeDefinition @"
using System;
public class Class1
{
public static void MyMethod(Action<object> obj)
{
obj("Hey!");
}
}
public class Class2
{
public static void Callback(object obj)
{
Console.WriteLine(obj.ToString());
}
}
"@
$method = [Class2].GetMethod("Callback")
$delegate = [System.Delegate]::CreateDelegate([System.Action[Object]], $null, $method)
[Class1]::MyMethod($delegate)
这篇关于提供 .NET 方法作为委托回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!