Search code examples
c#.netpowershellmockingmoles

How to add members to a PSObject in C#?


I'm using Microsoft Moles to mock a method.

This method call another method that invokes a PowerShell script and returns

Collection<PSObject>

So I want to mock it to return a custom PSObject. It will have two properties, ID and Name.

But when I try to use

PSObject obj = new PSObject();
obj.Members.Add(new PSMemberInfo(

I found the constructor is protected.

How can I add properties?

Thank you.


Solution

  • PSMemberInfo is a abstract class, so you cannot make an instance directly, but choose its subclass

    in the page : http://msdn.microsoft.com/en-us/library/system.management.automation.pspropertyinfo(v=vs.85).aspx you can find below:

    System.Object 
       System.Management.Automation.PSMemberInfo
        System.Management.Automation.PSPropertyInfo
           System.Management.Automation.PSAliasProperty
           System.Management.Automation.PSCodeProperty
           System.Management.Automation.PSNoteProperty
           System.Management.Automation.PSProperty
           System.Management.Automation.PSScriptProperty
    

    Choose your class, and init it.