I developed a PowerShell module that relied on a .NET Assembly for some operations.
I refactored this module to not need that Assembly and noticed some strange behavior, which blocks me from removing the class at last:
Add-Type -TypeDefinition "public interface ICanTalk { string talk(); }" -Language CSharp
class Talker : ICanTalk {
[string] talk() { return "Well hello there"; }
}
If you run this commands interactively, it will succeed.
But as soon as I run it "en-bloque" in ISE or from a psm1 file, it will throw an error, stating it cannot find the interface defined in the Add-Type
call.
I can reproduce the problem in both Windows-PowerShell and PowerShell Core (6.0.2)
What is the reason for the different behaviour and how can I tackle it?