gpt4 book ai didi

c# - 代码好像没有通过某行,导致StackOverflowException

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:12 24 4
gpt4 key购买 nike

我有一小段代码比较 2 个变量的类型,然后在它们是否相等时返回某个输出。代码如下:

public new bool Equals(object x, object y)
{
var xType = x.GetType();
var yType = y.GetType();
if (xType != yType) return false;

return GetPropertiesWithoutKey(xType).All(pd => Comparer.IsPropertyChanged(pd, x, y));
}

在调试时,我在'return GetPropertiesWithoutKey(...)' 上设置了一个断点,但在它到达该行后它不会越过它。即使按了几次 F10 和 F5。删除断点并按 F10 或 F5 后,我收到 StackOverflow 异常。

有人知道为什么会这样吗?

随时欢迎提出问题、意见和建议。

亲切的问候

编辑 1:GetPropertiesWithoutKey(...) 的代码

private IList<PropertyDescriptor> GetPropertiesWithoutKey(Type entityType)
{
return _syncProperties ??
(_syncProperties = GetEntityTypeDescriptor(entityType).Properties.Where(el => !el.IsKey).ToList());
}

编辑 2:Comparer.IsPropertyChanged(...) 的代码

public static bool IsPropertyChanged(PropertyDescriptor pd, object entity1, object entity2)
{
object val1 = null;
object val2 = null;
if ((typeof(ICollection).IsAssignableFrom(pd.PropertyInfo.PropertyType)
&& entity1 != null
&& ((ICollection)entity1).Count != 0)
|| (!typeof(ICollection).IsAssignableFrom(pd.PropertyInfo.PropertyType)
&& entity1 != null))
{
val1 = entity1;
//val1 = pd.PropertyInfo.GetValue(entity1);
}

if ((typeof(ICollection).IsAssignableFrom(pd.PropertyInfo.PropertyType)
&& entity2 != null
&& ((ICollection)entity2).Count != 0)
|| (!typeof(ICollection).IsAssignableFrom(pd.PropertyInfo.PropertyType)
&& entity2 != null))
{
val2 = entity2;
//val2 = pd.PropertyInfo.GetValue(entity2);
}

if (val1 == null && val2 == null)
{
return false;
}
if (val1 == null || val2 == null)
{
return true;
}
if (typeof(ICollection).IsAssignableFrom(pd.PropertyInfo.PropertyType))
{
return IsCollectionChanged(pd, val1, val2);
}
if (!pd.PropertyInfo.PropertyType.IsPrimitive || !(pd.PropertyInfo.PropertyType == typeof(String)))
{
return IsComplexChanged(pd, val1, val2); //<-- Debugger gets here (EDIT 3)
}

if (val1 != null)
{
return !val1.Equals(val2);
}
else if (val2 != null)
{
return !val2.Equals(val1);
}
return false;
}

编辑 3:IsComplexChanged 的​​代码

private static bool IsComplexChanged(PropertyDescriptor pd, object val1, object val2)
{
var comparer = new EqualityComparer();
return !comparer.Equals(val1, val2); //<-- This line refers back to Equals function
}

正如您在上次编辑中看到的,我已经找到了循环的位置。

首先,函数 GetChangedProperties() 调用 Comparer.IsPropertyChanged,在 IsPropertyChanged 调试器中,始终会到达 Comparer.IsComplexChanged,这指的是 Equals。在 GetPropertiesWithoutKey(...) 的 Equals 中的 lambda 表达式中,Comparer.IsPropertyChanged 被调用,然后创建循环。我已经写下了所采取步骤的时间表。

如果有人想看这个,尽管问我,我会拍张照片。

我希望有人可能知道解决方案或可以帮助我开始解决问题,非常感谢您的帮助,因为我现在正在画一个空白。

随时欢迎任何进一步的问题

最佳答案

Inside of the Equals method (or later IsPropertyChanged), you can't use the Equals or any variant of it (==, !=) (as it loops back and causes the stack-overflow), but in such cases you only need to compare against the null, you don't (that I could think of) need to compare the two objects (and you don't really need your custom Equals). So for those cases, just use the object.ReferenceEquals - which won't fire off your custom Equals (it uses the entirely different path).

您只需处理 entity != null 类型的检查(内部)——通常使用 object.ReferenceEquals 解决,例如

!object.ReferenceEquals(entity1, null)  

而不是
entity1 != null

关于c# - 代码好像没有通过某行,导致StackOverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26651420/

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