I need to find an equivelent to the following code for 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
The BindingFlags don’t seem to exist. The System.Reflection.PropertyInfo is a valid type, but I can’t figure out how to fill it. Any suggestions?
For Windows 8/Windows Phone 8, a lot of this Reflection functionality has been moved to a new TypeInfo class. You can find more information in this MSDN doc. For information including the runtime properties (including those that are inherited for example), you can use the new RuntimeReflectionExtensions class as well (where filtering can be done simply via LINQ).
Though this is C# code (my apologies :)), here is something fairly equivalent using this new functionality:
This code is even simpler if you only care about properties declared on the class itself: