需要帮助将c#代码转换为powershell脚本

时间:2018-03-30 08:32:56

标签: c# powershell scripting powershell-v2.0

我将此 c#代码转换为 powershell脚本

时遇到问题
var server = new WebSocketServer("ws://localhost:8181");
server.Start(socket =>
  {
    socket.OnOpen = () => Console.WriteLine("Open!");
    socket.OnClose = () => Console.WriteLine("Close!");
    socket.OnMessage = message => socket.Send(message);
  });

我已经在内存中加载了相关的dll,特别需要帮助理解和转换这段代码。

server.Start(socket =>
  {
    socket.OnOpen = () => Console.WriteLine("Open!");
    socket.OnClose = () => Console.WriteLine("Close!");
    socket.OnMessage = message => socket.Send(message);
  });

1 个答案:

答案 0 :(得分:1)

如果您已导入类型,则可以执行此操作。不确定它是否有效,因为我不知道你有哪些确切的参数类型。

$server = New-Object -TypeName WebSocketServer -ArgumentList "ws://localhost:8181"
$server.Start([Action[Socket]] { 
    param([Socket]$s) 
    $s.OnOpen = [Action] { Write-Host "Open!" } 
    $s.OnClose = [Action] { Write-Host "Close!" } 
    $s.OnMessage = [Action[string]] {
        param([string]$message)
        $s.Send($message)
    }

})