3

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

我正在创建一个在我的 UI 层中运行的函数,以便调试循环并将所有实体属性写入页面。它可以工作,只是当它到达导航属性时出现错误。我需要从循环中排除导航类型。

我尝试了 2 打方法来识别导航类型,但我不知道如何。

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
4

1 回答 1

3

尝试这个

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 
    }
于 2014-03-16T12:06:28.220 回答