Joe*_*ewd 5 c# powershell dictionary types expandoobject
我在 C# 中有一个ExpandoObject,它已经用大量的字段/属性进行了初始化,我想在 PowerShell 环境中使用这个对象。当我在 PowerShell 中检索这样的对象时,它不会按原样显示所有字段/属性,而是将它们(基于 ExpandoObjects 中的底层字典结构)显示为键/值对。
就我的实现而言,这是非常有问题的,我找不到任何方法将此键/值对转换为字段/属性,就像这样的对象应该表现的那样。将 ExpandoObject 转换为 Object 也不起作用。我错过了什么吗?
我的自定义 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);
}
Run Code Online (Sandbox Code Playgroud)
您将在下面找到我用来加载 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;
}
Run Code Online (Sandbox Code Playgroud)
我还没有找到如何正确地从 C# 到 PowerShell 进行转换,但我确实发现了在 PowerShell 中从 HashTables 构建对象的一个小技巧。关键在于使用 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;
}
Run Code Online (Sandbox Code Playgroud)
这确实有效,但有一些缺点。首先,它不保留我的类型信息。一切(除了集合)现在都是一个字符串,而不是 int、bool、float 等。第二个问题它可能不是最好和最干净的解决方案。我更喜欢处理 C# DLL 中的所有内容,以便为 PowerShell 用户保持尽可能抽象的低级功能。
这个解决方案可能有效,但我仍然需要一个更好的实现。
小智 5
到目前为止,我发现的最佳选择是在编写 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);
}
Run Code Online (Sandbox Code Playgroud)
您可以直接使用 PSObject,也可以编写一个简短的代码段来转换 ExpandoObject。鉴于您所描述的场景似乎有更深的对象嵌套,转换代码段可能是更好的方法。
| 归档时间: |
|
| 查看次数: |
2318 次 |
| 最近记录: |