none
Setting custom string values in PowerShell engine or host using C# RRS feed

  • Question

  • Hi,

    I have a C# module in which I have created some Cmdlets (say MyCmdlet derived from PSCmdlet). These Cmdlets need some custom information which is set into a Host class (say MyHost derived from PSHost in Module A). I need access to this custom data in MyHost class from my Cmdlets (in Module B). 
    In MyCmdlet class, in its ProcessRecord() method, I can get access to:
    PSHost hst = this.Host; 

    I need to type cast this to MyHost. I'm not sure if that's the correct approach since I need to add a reference to the module which implements MyHost (A). The MyHost module in turn needs to add a reference to module B since it imports the Cmdlet module (B) like this:

    using (PowerShell pShell = PowerShell.Create()) {
    pShell.AddCommand("Import-Module").AddArgument(@"C:\Module_B.dll");
    ....
    }

    In that case how do I access data in MyHost class from MyCmdlet class?

    TIA,
    Jy


    Wednesday, March 21, 2018 1:53 PM

All replies

  • Hello Jy_13,

    >>I need to add a reference to the module which implements MyHost (A). The MyHost module in turn needs to add a reference to module B since it imports the Cmdlet module (B) like this

    It sounds that you have two modules which want to refer each other. It will cause circular dependency issue.  A circular dependency is where Project A depends on something in Project B and project B depends on something in Project A. This means to compile Project A you must first compile Project B, but you can't do that as B requires A to be compiled. This is the problem that circular dependencies cause.

    As for a workaround, you could define a thrid party module "C", which contains the classes that both A and B depend on so they no longer depend on each other.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 22, 2018 7:38 AM
    Moderator