(环境:具有N层架构的EF6 FW4.5 Webforms应用程序)
我正在创建一个在我的UI层运行的函数,以便调试循环并将所有实体属性写入页面。除了我到达导航属性时收到错误,它才有效。我需要从循环中排除导航类型。
我尝试了24种方法来识别导航类型,但我不知道如何。
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
答案 0 :(得分: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
}