-1

I am working on an asp.net mvc-5 web application. and i am calling some power-shell scripts inside my web application, and populate a dynamic object with the powershell scripts results.. here is part of the results i get from running powershell scripts :-

enter image description here

can anyone adivce how i can access for example the BaseObject.Build value? i tried the following results[0].BaseObject.Build but i got a null reference exception . as results[0].BaseObject will be null! can anyone adivce on this please ?

Community
  • 1
  • 1
John John
  • 1
  • 72
  • 238
  • 501
  • 1
    What do you see in the intellisense when you do `results[0].` I would imagine you would see a `Build` property. try `results[0].Build.ToString()` If you could create a https://dotnetfiddle.net/ it would help. – mwilson Oct 14 '15 at 00:09
  • @mwilson the intellisense will not show any thing as this is a dynamic object (build at runtime),, so i assume the intellisense will not be able to know any thing about it in advance is this correct ? – John John Oct 14 '15 at 01:08
  • @mwilson now i wrote the following results[0].Build and it worked well on our Live environment which have Windows Server 2008 r2 + IIS-8 ,, while on our Dev environment which have Windows Server 2008 R" +IIS-7 i need to reference it using results[0].BaseObject.Build ... not sure why ? – John John Oct 14 '15 at 01:40

2 Answers2

1

you can use this method

public static object GetPropValue(object src, string propName)
{
   return src.GetType().GetProperty(propName).GetValue(src, null);
}

call this method by GetPropValue(results[0], "Build")

it will returns Build value

for more information return to this post Get property value from string using reflection in C#

Community
  • 1
  • 1
Hady Allam
  • 128
  • 9
0

Try to get BaseObject with reflection, like:

results2.GetType().GetProperty("BaseObject").GetValue(results2, null)

or

results2.GetType().GetField("BaseObject").GetValue(results2, null) .

LeftyX
  • 35,328
  • 21
  • 132
  • 193
Dexion
  • 1,101
  • 8
  • 14