Pregunta

(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
¿Fue útil?

Solución

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 
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top