Using Reflection to Get the Value of a Nested Property
I recently ran into a situation wherein I needed to use reflection to get the value of a nested property. We were returning a custom type that inherits from JsonResponse, but this custom type is private to the controller class that uses it (I'm not sure why -- since we're returning it from some action methods I'd have thought it could be public. But I digress). When writing some unit tests for these methods, I wanted to check the value of the success property which just so happens to be nested within the Data property. So I wrote a little method to assist with this, as shown below (the method and class names have been changed to protect the innocent). The steps to follow are: Get the type of the object. Using the type, get the parent property of the nested property you want via Type.GetProperty(), specifying the name of the parent property as the parameter. Using the property, get its value using PropertyInfo.GetValue(), specifying the parent object and null as pa