15

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]]
3
  • Where is MyType defined? You likely need to load its assembly, or qualify it with a namespace. Commented Nov 8, 2013 at 1:11
  • $wow2 works though. why would wow2 work and not wow3? This is just example, in the actual script I put the full name Commented Nov 8, 2013 at 1:12
  • Will a ScriptBlock work? technet.microsoft.com/en-us/library/hh847893.aspx Commented Nov 8, 2013 at 2:59

1 Answer 1

46

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.