0

I need a function with the following signature:

System.Reflection.PropertyInfo getPropertyInfo(System.Type type, string NavigationPath)

or in VB:

Function GetPropertyInfo(Type As System.Type, NavigationPath As String) As System.Reflection.PropertyInfo

Usage:

Dim MyPropertyInfo As PropertyInfo = GetPropertyInfo(GetType(Order),"Customer.Address.State.Code")
Dim DisplayName As String = MyStringFunctions.FriendlyName(MyPropertyInfo.Name)

It uses period-delimited path navigation. I can't figure out how to harness the databinding framework to do this. First hurdle is it seems only to want to work with objects (not types), second hurdle is I couldn't even get it to work with objects outside of a control. I would think under the hood somewhere databinding deals with types and property types; it would have to!

Thanks!

toddmo
  • 20,682
  • 14
  • 97
  • 107

2 Answers2

0

For data binding you'd want the property's value rather than the property itself (i.e. return object rather than PropertyInfo).

This similar question provides such implementation which can also be easily modified in case you actually want the method to return the property and not its value.

Community
  • 1
  • 1
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
  • I wasn't trying to do databinding. I was trying to leverage the databinding infrastructure to implement this function. – toddmo Jun 26 '12 at 20:03
0

Here's what I came up with, that doesn't use binding infrastructure. I still think binding would do a better job.

  Function GetPropertyInfo(Type As System.Type, NavigationPath As String) As System.Reflection.PropertyInfo
    For Each Part As String In NavigationPath.Split(".")
      GetPropertyInfo = Type.GetProperty(Part)
      If GetPropertyInfo IsNot Nothing Then
        Type = GetPropertyInfo.PropertyType
      End If
    Next
  End Function
toddmo
  • 20,682
  • 14
  • 97
  • 107