gpt4 book ai didi

entity-framework - 识别和筛选实体数据对象中的导航属性

转载 作者:行者123 更新时间:2023-12-04 07:25:57 26 4
gpt4 key购买 nike

(环境:具有 N 层架构的 EF6 FW4.5 Webforms 应用程序)

我正在创建一个在我的 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

最佳答案

试试这个

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
}

关于entity-framework - 识别和筛选实体数据对象中的导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22431903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com