1

Adding a delegate to C# code in powershell seems to fail it, and my question is why and if there is any workarounds to get this working (other than using Func or Action)

Testcase:

$id = get-random
$tester = Add-Type -TypeDefinition @"
using System;

public static class Class$id {
    public static void test()
    {
        Console.WriteLine("test$id");
    }

    private delegate void doStuff(); // comment out this line and we are ok?
}
"@ -Language CSharp -PassThru
$tester
$tester::test()

Results in

PS C:\> $tester

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    Class897305760                           System.Object
False    True     doStuff                                  System.MulticastDelegate


PS C:\> $tester::test()
Method invocation failed because [System.Object[]] does not contain a method named 'test'.
At line:1 char:1
+ $tester::test()
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Removing or commenting out private delegate void doStuff(); instead runs ok:

PS C:\> $tester

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    Class883741893                           System.Object


PS C:\> $tester::test()
test883741893

Here we can see that doStuff is available outside the class, not sure if that is relevant in some way.

I do intend to use the delegate, but that part of the code is not needed to reproduce the issue, so left it out. My current workaround is to use Action<> instead, but in this case it reduces self documentation which I would like to have if at all possible.

1 Answer 1

2

Add-Type returns both the Class$ID type as well as the delegate type - so $tester is an array containing both:

$class = $tester |Where Name -like 'Class*' |Select -First 1
$class::test() # this should work now, with or without the delegate
Sign up to request clarification or add additional context in comments.

3 Comments

The only thing left to have explained is why the delegate is included in this unexpected way. (why not functions, enums or other parts)
@NiKiZe It's not unexpected - Add-Type -PassThru returns all types - including classes, enums, delegates. Try Add-Type 'public enum A{} public enum B{}'
The lovely inner structures of MSIL i guess? The unexpected (inconsistency) for me is that we get one class, and then a delegate, even if that delegate is in the class. since the delegate is there on the same level as the class, I would have expect the methods to be on the same level as well.

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.