Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在没有底层字典结构的PowerShell中使用C#ExpandoObjects(动态)_C#_Powershell_Types_Dictionary_Expandoobject - Fatal编程技术网

在没有底层字典结构的PowerShell中使用C#ExpandoObjects(动态)

在没有底层字典结构的PowerShell中使用C#ExpandoObjects(动态),c#,powershell,types,dictionary,expandoobject,C#,Powershell,Types,Dictionary,Expandoobject,我在C#中有一个ExpandoObject,它已用大量字段/属性初始化,我希望在PowerShell环境中使用此对象。当我在PowerShell中检索这样一个对象时,它不会显示所有字段/属性,但会将它们(基于ExpandoObjects中的底层字典结构)显示为键/值对 就我的实现而言,这是一个相当有问题的问题,我找不到任何方法将这个键/值对转换为像这样一个对象应该表现的字段/属性。将ExpandoObject强制转换为Object也不起作用。我错过什么了吗 自定义DLL(DataCollect.

我在C#中有一个ExpandoObject,它已用大量字段/属性初始化,我希望在PowerShell环境中使用此对象。当我在PowerShell中检索这样一个对象时,它不会显示所有字段/属性,但会将它们(基于ExpandoObjects中的底层字典结构)显示为键/值对

就我的实现而言,这是一个相当有问题的问题,我找不到任何方法将这个键/值对转换为像这样一个对象应该表现的字段/属性。将ExpandoObject强制转换为Object也不起作用。我错过什么了吗

自定义DLL(DataCollect.DLL)中的合并函数

在下面,您将找到我的PowerShell脚本,我使用它加载我的C#dll并调用合并函数

以及我的PS命令的结果:



更新

我还没有找到如何正确地从C#转换到PowerShell的方法,但我确实发现了一个从哈希表在PowerShell中构建对象的小技巧。关键在于使用Add-Member cmdlet,该cmdlet允许您在基本对象(例如System.object)上动态构建对象

因此,我构建了一个模块,该模块从哈希表递归地构建对象(我使用递归方法,因为属性也可以是哈希表(或ExpandoObjects))

这确实有效,但有一些缺点。首先,它不保存我的类型信息。所有内容(除了集合)现在都是字符串,而不是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);
}
while(reader.Read())
{
var记录=新的PSObject();
对于(变量i=0;i
您可以直接使用PSObject,也可以编写一个简短的代码段来转换ExpandooObject。考虑到您所描述的场景,其中似乎存在更深层次的对象嵌套,转换片段可能是更好的方法

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