[英]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. 我在C#中有一个ExpandoObject ,它已经被大量的字段/属性初始化,我想在PowerShell环境中使用这个对象。 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. 当我在PowerShell中检索这样的对象时,它不会显示所有应该是的字段/属性,但它会将它们(基于ExpandoObjects中的基础字典结构)显示为键/值对。
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. 出于我的实现的目的,这是非常有问题的,我找不到任何方法将这个Key / Value配对转换为像这样的对象应该表现的字段/属性。 Casting the ExpandoObject to Object doesn't work either. 将ExpandoObject强制转换为对象也不起作用。 Am I Missing something? 我错过了什么吗?
Merge Function in my Custom Made DLL (DataCollect.dll) 我的自定义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. 下面你会找到我用来加载我的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;
}
And the result of my PS commands: 和我的PS命令的结果:
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. 我还没有找到如何正确地从C#转换到PowerShell的转换,但我确实发现了一个从HashTables在PowerShell中构建对象的小技巧。 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). 关键是使用Add-Member cmdlet,它允许您在基础对象(例如System.Object)上动态构建对象。
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)). 所以我构建了一个从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; }
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. 一切(除了集合)现在是一个字符串而不是int,bool,float等。第二个问题可能不是最好和最干净的解决方案。 I prefer to handle everything inside the C# DLL in order to keep the low level functionality as abstract as possible for PowerShell users. 我更喜欢处理C#DLL中的所有内容,以便为PowerShell用户保持尽可能抽象的低级功能。
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. 到目前为止,我发现的最佳选择是在编写cmdlet时使用PSObject。 On a PSObject, you can add arbitrary PSVariable objects to do basically the same thing. 在PSObject上,您可以添加任意PSVariable对象来执行基本相同的操作。 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. 您可以直接使用PSObject,也可以编写一个简短的片段来转换ExpandoObject。 Given your described scenario where there appears to be deeper nesting of objects, a conversion snippet might be the better way to go. 鉴于您所描述的场景似乎有更深层次的对象嵌套,转换代码段可能是更好的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.