Access PSObject property by name in C#

一世执手 提交于 2020-12-02 06:00:49

问题


For example I have a PSObject transaction with two properties: id and transactionName , so that it looks like: transaction { id: 123 transactionName : tranName1 }

and I want to return the id of the transaction if its name is tranName1.

It looks to me that in powershell scripts, we can simply do:

if $transaction.transactionName -eq tranName return $transaction.id

however in c# it will give error since it cannot recognize the property by name... any ideas how to do it in c#?


回答1:


Try something like this:

psobjectvariable.Properties["transactionName"].Value



回答2:


Here's something that I didn't expect to work, but it did.

dynamic x = psobjectvariable;
Console.Write(x.transactionName);


来源:https://stackoverflow.com/questions/15278228/access-psobject-property-by-name-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!