I am trying implement the Data transformation using Reflection1 example in my code.
The GetSourceValue
function has a switch comparing various types, but I want to remove these types and properties and have GetSourceValue
get the value of the property using only a single string as the parameter. I want to pass a class and property in the string and resolve the value of the property.
Is this possible?
If I use the code from Ed S. I get
It seems that
GetProperty()
is not available in Xamarin.Forms.TargetFrameworkProfile
isProfile7
in my Portable Class Library (.NET Framework 4.5, Windows 8, ASP.NET Core 1.0, Xamarin.Android, Xamarin.iOS, Xamarin.iOS Classic).Now I found a working solution:
Source
The following code is a Recursive method for displaying the entire hierarchy of all of the Property Names and Values contained in an object's instance. This method uses a simplified version of AlexD's
GetPropertyValue()
answer above in this thread. Thanks to this discussion thread, I was able to figure out how to do this!For example, I use this method to show an explosion or dump of all of the properties in a
WebService
response by calling the method as follows:Of course, you will want to add validation and whatnot, but that is the gist of it.
The method to call has changed in .NET Standard (as of 1.6). Also we can use C# 6's null conditional operator.
Using PropertyInfo of the System.Reflection namespace. Reflection compiles just fine no matter what property we try to access. The error will come up during run-time.
It works fine to get the Location property of an object
We'll get the Location : {X=71,Y=27} We can also return location.X or location.Y on the same way.
About the nested properties discussion, you can avoid all the reflection stuff if you use the
DataBinder.Eval Method (Object, String)
as below:Of course, you'll need to add a reference to the
System.Web
assembly, but this probably isn't a big deal.