在C#中有一个ExpandoObject已经用大量字段/属性初始化,我想在PowerShell环境中使用此对象。当我在PowerShell中检索这样一个对象时,它不会像应该那样显示所有的字段/属性,但它会根据键/值对显示它们(基于ExpandoObjects中的基础字典结构)。在没有底层字典结构的PowerShell中使用C#ExpandoObjects(动态)
为了实现我的目的,这是相当成问题的,我找不到任何方法将这个键/值配对转换为像这样的对象应该表现的字段/属性。将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和调用合并功能我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的结果命令: a imagelink http://img29.imageshack.us/img29/6958/vraag5powershelloutput.png
更新
我还没有找到如何正确地进行转换,从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用户保留尽可能抽象的低级功能。
这个解决方案可能工作,但我仍然需要更好的实现。