3

(Environment: EF6 FW4.5 Webforms Application with N-Tier architecture)

I am creating a function to run in my UI layer for debug to loop and write all entity properties to the page. It works except that I get an error when it gets to a Navigation Property. I need to exclude navigation type from the loop.

I tried 2 dozen ways to identity the navigation types, but I don't know how.

Public Shared Function iterateEFObjectProperties(ByVal o As Object) As String
    Dim str As String = ""
    Dim sb As New StringBuilder
    For Each p As System.Reflection.PropertyInfo In o.GetType().GetProperties(BindingFlags.DeclaredOnly Or BindingFlags.[Public] Or BindingFlags.Instance)

        'TODO: This gives an error when attempting to write the Navigation Properties of the object.
        ' Need to filter those with an IF statement in this loop
        If p.CanWrite Then
            'str = "{0}: {1}" & ", " & p.Name.ToString & ", " & p.GetValue(o, Nothing).ToString & "<br>"
            sb.Append(str)
        End If
    Next
    str = sb.ToString
    Return (str)
End Function
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
Ted
  • 123
  • 3
  • 13
  • [Here's](http://stackoverflow.com/q/21709302/861716) how you can find non-navigation property names (if you remove the filter on identity). – Gert Arnold Mar 16 '14 at 13:06
  • If you have found the answer to this question, please add it as an answer (and only an answer) to this question. There is no need to edit your question to include the answer. – The Guy with The Hat Mar 27 '14 at 20:17
  • I was following what I've seen being practiced. I did mark the answer below as THE ANSWER. I only edited it to help someone else so they could see the final form. Did you remove the edit? WTF. Why didn't you just paste it and show me what I should have done? not delete it. I came back and edited that final code 3x to make sure it complete and right! – Ted Mar 28 '14 at 07:26

1 Answers1

3

Try this

if ((propertyType.IsClass && propertyType!= typeof(string)) ||
     (propertyType.IsArray || (typeof(IEnumerable).IsAssignableFrom(propertyType) && propertyType != typeof(string))))
    {
       // Then do your job this property is what you want 
    }
Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106