簡體   English   中英

在PowerShell中使用C#ExpandoObjects(動態)而沒有基礎的Dictionary結構

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

我在C#中有一個ExpandoObject ,它已經被大量的字段/屬性初始化,我想在PowerShell環境中使用這個對象。 當我在PowerShell中檢索這樣的對象時,它不會顯示所有應該是的字段/屬性,但它會將它們(基於ExpandoObjects中的基礎字典結構)顯示為鍵/值對。

出於我的實現的目的,這是非常有問題的,我找不到任何方法將這個Key / Value配對轉換為像這樣的對象應該表現的字段/屬性。 將ExpandoObject強制轉換為對象也不起作用。 我錯過了什么嗎?

我的自定義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);
}

下面你會找到我用來加載我的C#dll並調用merge函數的PowerShell腳本。

#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;    
}

和我的PS命令的結果: 一個imagelink



更新

我還沒有找到如何正確地從C#轉換到PowerShell的轉換,但我確實發現了一個從HashTables在PowerShell中構建對象的小技巧。 關鍵是使用Add-Member cmdlet,它允許您在基礎對象(例如System.Object)上動態構建對象。

所以我構建了一個從HashTables遞歸構建對象的模塊(我使用遞歸方法,因為屬性也可以是HashTables(或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; } 

這確實有效,但有一些缺點 首先,它不保留我的類型信息。 一切(除了集合)現在是一個字符串而不是int,bool,float等。第二個問題可能不是最好和最干凈的解決方案。 我更喜歡處理C#DLL中的所有內容,以便為PowerShell用戶保持盡可能抽象的低級功能。

此解決方案可能有效,但我仍需要更好的實現。

到目前為止,我發現的最佳選擇是在編寫cmdlet時使用PSObject。 在PSObject上,您可以添加任意PSVariable對象來執行基本相同的操作。 例如:

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);
}

您可以直接使用PSObject,也可以編寫一個簡短的片段來轉換ExpandoObject。 鑒於您所描述的場景似乎有更深層次的對象嵌套,轉換代碼段可能是更好的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM