locked
How to Convert "Dim powershellresult As System.Collections.ObjectModel.Collection(Of PSObject)" to C#? RRS feed

  • Question

  • User-1651604128 posted

    Hi,

    I need to convert this VB code to C#, can anybody help?

    I have tried this, but it does not compile.

    PSObject powershellresult = new System.Collections.ObjectModel.Collection<PSObject>;

    or PSObject powershellresult = new System.Collections.ObjectModel.Collection<PSObject>();

    any idea?

    Thursday, October 3, 2019 4:54 PM

Answers

  • User-17257777 posted

    Hi Peter

    First, you should reference the “Microsoft.PowerShell.5.ReferenceAssemblies”.

    https://www.nuget.org/packages/Microsoft.PowerShell.5.ReferenceAssemblies/

    And the reason why the compilation fails is inconsistent type.

    From your description, I think you want to define “powershellresult” as a collection type, so you can change it like below:

    System.Collections.ObjectModel.Collection<PSObject> powershellresult = new System.Collections.ObjectModel.Collection<PSObject>();

    Or you can simply use `var` to ask the compiler to infer the type:

    var  powershellresult = new System.Collections.ObjectModel.Collection<PSObject>();

    Best Regards,

    Jiadong Meng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 4, 2019 7:03 AM
  • User1120430333 posted

    PSObject powershellresult = new System.Collections.ObjectModel.Collection<PSObject>;

    PSObject is not a collection and is most likely just a single object. So you cannot cast a collection of PSobject(s) to be a PSObject, becuase PSObject is not a collection.

    System.Collections.ObjectModel.Collection<PSObject> powershellresult = new System.Collections.ObjectModel.Collection<PSObject>;

    the above may work

    OR

    var powershellresult = new System.Collections.ObjectModel.Collection<PSObject>; // strong types the object.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 4, 2019 7:47 AM

All replies

  • User-17257777 posted

    Hi Peter

    First, you should reference the “Microsoft.PowerShell.5.ReferenceAssemblies”.

    https://www.nuget.org/packages/Microsoft.PowerShell.5.ReferenceAssemblies/

    And the reason why the compilation fails is inconsistent type.

    From your description, I think you want to define “powershellresult” as a collection type, so you can change it like below:

    System.Collections.ObjectModel.Collection<PSObject> powershellresult = new System.Collections.ObjectModel.Collection<PSObject>();

    Or you can simply use `var` to ask the compiler to infer the type:

    var  powershellresult = new System.Collections.ObjectModel.Collection<PSObject>();

    Best Regards,

    Jiadong Meng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 4, 2019 7:03 AM
  • User1120430333 posted

    PSObject powershellresult = new System.Collections.ObjectModel.Collection<PSObject>;

    PSObject is not a collection and is most likely just a single object. So you cannot cast a collection of PSobject(s) to be a PSObject, becuase PSObject is not a collection.

    System.Collections.ObjectModel.Collection<PSObject> powershellresult = new System.Collections.ObjectModel.Collection<PSObject>;

    the above may work

    OR

    var powershellresult = new System.Collections.ObjectModel.Collection<PSObject>; // strong types the object.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 4, 2019 7:47 AM