How to use reflection in Portable Class Library for Windows Store / WP8 / WinRT?

I need to find the equivalent following code to use in a portable library:

Public Overridable Function GetPropertyValue(ByVal p_propertyName As String) As Object Dim bf As System.Reflection.BindingFlags bf = Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Dim propInfo As System.Reflection.PropertyInfo = Me.GetType().GetProperty(p_propertyName, bf) Dim tempValue As Object = Nothing If propInfo Is Nothing Then Return Nothing End If Try tempValue = propInfo.GetValue(Me, Nothing) Catch ex As Exception Errors.Add(New Warp10.Framework.BaseObjects.BaseErrorMessage(String.Format("Could not Get Value from Property {0}, Error was :{1}", p_propertyName, ex.Message), -1)) Return Nothing End Try Return tempValue End Function 

BindingFlags does not seem to exist. System.Reflection.PropertyInfo is a valid type, but I cannot figure out how to populate it. Any suggestions?

+4
source share
1 answer

For Windows 8 / Windows Phone 8, most of this Reflection feature has been ported to the new TypeInfo class. You can find more information in this MSDN document . For information, including runtime properties (including those that are inherited, for example), you can also use the new RuntimeReflectionExtensions class (where filtering can be done simply through LINQ).

Although this is C # code (my apologies :)), here is something pretty equivalent using this new functionality:

 public class TestClass { public string Name { get; set; } public object GetPropValue(string propertyName) { var propInfo = RuntimeReflectionExtensions.GetRuntimeProperties(this.GetType()).Where(pi => pi.Name == propertyName).First(); return propInfo.GetValue(this); } } 

This code is even simpler if you only care about the properties declared in the class itself:

 public class TestClass { public string Name { get; set; } public object GetPropValue(string propertyName) { var propInfo = this.GetType().GetTypeInfo().GetDeclaredProperty(propertyName); return propInfo.GetValue(this); } } 
+7
source