问题
我目前正在创建一个应用程序。在这个应用程序中,我正在处理一个 Func 的序列化。这不知何故使我的应用程序无一异常(exception)地崩溃了。
无一异常(exception)的崩溃让我对 wtf 正在发生感到好奇,所以我做了一些深入的研究,经过一些挖掘终于发现在 Newtonsoft.Json 的某个地方发生了 List.Contains
,然后对 2 个属性执行相等检查。
显然,此等式检查会导致无限循环,从而导致 stackoverflow 异常。
仅使用 C# 重现问题
Expression<Func<string, int>> expr = (t) => t.Length;
Func<string, int> exprCompiled = expr.Compile();
var aa = exprCompiled.Method.Module;
var bb = exprCompiled.Method.Module.Assembly;
//This code results in either an infinite loop or a Stackoverflow Exception
var tempresult = aa.Equals(bb);
Console.WriteLine("This code is never executed");
使用 Newtonsoft.Json 重现问题
Expression<Func<string, int>> expr = (t) => t.Length;
Func<string, int> exprCompiled = expr.Compile();
//This code results in either an infinite loop or a Stackoverflow Exception
var res = JsonConvert.SerializeObject(exprCompiled);
Console.WriteLine("This code is never executed");
实际的潜在问题
深入研究 .NET 框架的工作原理 我认为问题在于 InternalAssemblyBuilder
内部类和 InternalModuleBuilder
内部类的实现。它们都有一个 Equals 方法重写,如下所示:
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
if (obj is InternalAssemblyBuilder)
{
return this == obj;
}
return obj.Equals(this);
}
我觉得应该是这样的:
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
if (obj is InternalAssemblyBuilder)
{
return this == obj;
}
return base.Equals(this); //changed obj to base
}
最佳答案
如您问题的实际潜在问题以及NineBerry所述在 comments , Microsoft 的实现 InternalAssemblyBuilder.Equals(object)
和 InternalModuleBuilder.Equals(object)
似乎坏了。具体来说,在检查 InternalAssemblyBuilder
类型的对象之间是否相等的情况下和一个 InternalModuleBuilder
类型的对象,将发生无限递归。
要解决此问题,您可以设置自定义 IEqualityComparer
在 JsonSerializer.SettingsEqualityComparer
替代 Equals()
的合理实现对于这些类型。下面是一个使用引用相等的例子:
public class CustomJsonEqualityComparer : IEqualityComparer
{
public static readonly CustomJsonEqualityComparer Instance = new CustomJsonEqualityComparer();
// Use ImmutableHashSet in later .net versions
static readonly HashSet<string> naughtyTypes = new HashSet<string>
{
"System.Reflection.Emit.InternalAssemblyBuilder",
"System.Reflection.Emit.InternalModuleBuilder"
};
static readonly IEqualityComparer baseComparer = EqualityComparer<object>.Default;
static bool HasBrokenEquals(Type type)
{
return naughtyTypes.Contains(type.FullName);
}
#region IEqualityComparer Members
public bool Equals(object x, object y)
{
// Check reference equality
if ((object)x == y)
return true;
// Check null
else if ((object)x == null || (object)y == null)
return false;
var xType = x.GetType();
if (xType != y.GetType())
// Types should be identical.
// Note this check alone might be sufficient to fix the problem.
return false;
if (xType.IsClass && !xType.IsPrimitive) // IsPrimitive check for performance
{
if (HasBrokenEquals(xType))
{
// These naughty types should ONLY be compared via reference equality -- which we have already done.
// So return false
return false;
}
}
return baseComparer.Equals(x, y);
}
public int GetHashCode(object obj)
{
return baseComparer.GetHashCode(obj);
}
#endregion
}
然后您将按如下方式使用它:
var settings = new JsonSerializerSettings
{
EqualityComparer = CustomJsonEqualityComparer.Instance,
};
var json = JsonConvert.SerializeObject(exprCompiled, settings);
注意事项:
您可能需要调整
CustomJsonEqualityComparer.HasBrokenEquals()
如果其他System.Reflection.Emit
类型同样破坏了Equals()
的实现.确保两个传入对象具有相同的
System.Type
可能就足够了GetType()
的值, 作为 splinterEquals()
到目前为止发现的方法只会在比较两个不同类型并且都具有相同错误的情况下溢出堆栈。虽然我能够重现无限递归并验证它已使用一些模型对象修复,但我无法确认 Json.NET 是否真的可以序列化您的
Func<string, int> exprCompiled
.
关于c# - 为什么相互比较 2 个 .NET 框架类会导致 stackoverflow 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54929372/