简体   繁体   中英

Using C# ExpandoObjects (Dynamic) in PowerShell without underlying Dictionary structure

I have an ExpandoObject in C# that has been initialized with a great deal of fields/properties and I want to use this object in a PowerShell environment. When I retrieve such an object in PowerShell it doesn't display all the fields/properties as they should be, but it displays them (based on the underlying dictionary structure in ExpandoObjects) as Key/Value pairs.

For the purpose of my implementation this is quite problematic and I couldn't find any means to convert this Key/Value pairing into fields/properties like such an object should behave. Casting the ExpandoObject to Object doesn't work either. Am I Missing something?

Merge Function in my Custom Made DLL (DataCollect.dll)

public static dynamic merge(dynamic obj)
{
    // FIRST  : Get the objects out of the table layout.
    dynamic Data = retrieveObject(obj);

    // SECOND : Check if we're dealing with an array or with a single object.
    bool isCollect = isCollection(Data);

    // THIRD  : Merge objects differently depending on (bool)isCollect.
    // The functions below are merge functions that make use of the ExpandoObject's 
    // underlying Dictionary structure to display it's internal fields/properties.
    if (isCollect)
        return (Object)mergeObjectCollection(Data);
    else
        return (Object)mergeObject(Data);
}

Below you'll find my PowerShell script I use to load my C# dll and call the merge function.

#Loads in the custom DLL created for this specific project.
[Reflection.Assembly]::LoadFrom("blablbabla/DataCollect.dll")

# Creates a new Client object that handles all communication between the PowerShell module and the
# sncdb-worker at server side.
$client = new-object blablbabla.Sender;
[blablbabla.Config]::Configure("blablbabla/blablbabla.ini")
$client.Connect();

# This functions returns a Host Machine (Virtual or Physical) in object notation to for easy post-processing
# in PowerShell. 
Function SNC-GetHost($hostz = "blablbabla")
{
    $obj = $client.sendMessage([blablbabla.Parser]::getHostIp($hostz)); 
    return ([blablbabla.Merger]::merge($obj)).Value;    
}

And the result of my PS commands: 一个imagelink



Update

I haven't found out how to do the conversion properly from C# to PowerShell yet, but I did found a small trick in building objects in PowerShell from HashTables. The key is in using the Add-Member cmdlet which lets you build objects dynamically on top of a base object (System.Object for example).

So I built a module that builds an object recursively from the HashTables (I'm using a recursive method since properties can be HashTables as well (or ExpandoObjects)).

 ############################################################################# # PowerShell Module that supplements the DataCollector Library. # # Generated on: 8/7/2012 Last update: 8/17/2012 # ############################################################################# function HashToObject($hash) { # Create a placeholder object $object = New-Object System.Object; # Dynamically add Properties to our Placeholder object from the HashTable $hash | foreach { $object | Add-Member NoteProperty $_.Key $_.Value } # Search for collections and recursively expand these collections to # objects again. $object | Get-Member | foreach { if($_.Definition.StartsWith("System.Dynamic.ExpandoObject")) { Write-Host "Recursively continued on object: " -foregroundcolor DarkGreen -nonewline Write-Host $_.Name -foregroundcolor Yellow $object.($_.Name) = HashToObject($object.($_.Name)) } } return $object; } 

This does actually work, but has a few drawbacks . First of all it doesn't keep my Type information. Everything (besides collections) is now a string instead of int, bool, float etc. The second problem it's probably not the best and most clean solution. I prefer to handle everything inside the C# DLL in order to keep the low level functionality as abstract as possible for PowerShell users.

This solution might work, but I still need a better implementation.

The best option I have found so far is to use PSObject when writing cmdlets. On a PSObject, you can add arbitrary PSVariable objects to do basically the same thing. For example:

while (reader.Read())
{
    var record = new PSObject();
    for (var i = 0; i < reader.FieldCount; i++)
    {
        record.Properties.Add(new PSVariableProperty(
            new PSVariable(reader.GetName(i), reader.GetValue(i))
            ));
    }

    WriteObject(record);
}

You could either us PSObject directly, or you could write a short snippet to convert the ExpandoObject. Given your described scenario where there appears to be deeper nesting of objects, a conversion snippet might be the better way to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM