Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell how to create a delegate

Tags:

powershell

I am not to familar with powershell. I am trying to do something that would be a single line of code in C# but in powershell it is a pain :(

In the three lines below wow3 throws an error. Anyone know why wow3 throw a type not found error? Does this syntax for delegates only work for built in types?

$wow1 =[System.Action[int]]
$wow2 =[MyType]
$wow3 =[System.Action[MyType]]
like image 344
user1985513 Avatar asked Nov 08 '13 01:11

user1985513


1 Answers

This line of PowerShell:

$wow1 = [System.Action[int]]

is equal to this line of C#:

var d = typeof(System.Action<int>);

That is, $wow1 contains a System.RuntimeType. Is that really what you are trying to do?

Perhaps you want something like this instead?

C:\PS> [Action[int]]$action = {param($i) Write-Host "i is $i"}
C:\PS> $action.Invoke(10)
i is 10
like image 193
Keith Hill Avatar answered Sep 19 '22 08:09

Keith Hill