Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value from dynamic property

Tags:

c#

d.GetType().GetProperty("value2").GetValue(d, null);

this returns the value of the value2 property inside d.

I want the value of the property inside value2 i.e d.value2.value3.

How should i achieve this?

like image 972
R B Avatar asked Mar 05 '26 05:03

R B


2 Answers

object e = d.GetType().GetProperty("value2").GetValue(d, null);
object f = e.GetType().GetProperty("value3").GetValue(e, null);
like image 67
SLaks Avatar answered Mar 07 '26 19:03

SLaks


In C# 4, simply use the dynamic keyword to allow run time evaluation of your properties:

((dynamic) d).value2.value3;
like image 42
Julien Lebosquain Avatar answered Mar 07 '26 17:03

Julien Lebosquain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!