我的代码使用城堡DynamicProxy来代理代码调用。在Intercept(IInvocation调用)中,我使用NewtonSoft来序列化调用。
Newtonsoft.Json.JsonConvert.SerializeObject(invocation.Method);在框架中,这会产生一个非常简洁的东西,如下所示:
{
"Name": "FastLoadDataAsJson",
"AssemblyName": "TinyData, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"ClassName": "Dimension2.Core.Database.TinyData",
"Signature": "Byte[] FastLoadDataAsJson(System.String, System.String, System.String)",
"Signature2": "System.Byte[] FastLoadDataAsJson(System.String, System.String, System.String)",
"MemberType": 8,
"GenericArguments": null
}在.Net核心项目中,相同的序列化调用首先给出此异常:
自引用循环检测到属性'ManifestModule‘类型为'System.Reflection.RuntimeModule’。路径‘模组’
我可以使用设置Newtonsoft.Json.ReferenceLoopHandling.Ignore绕过它
但是我得到的JSON是93,000行长!到底是怎么回事?似乎调用完全不同。除了长度之外,属性是不同的,例如,没有签名或Signature2属性。
框架简洁的Json似乎完全适合描述我们需要进行的调用。核心为何如此不同?我担心我错过了一些关于城堡,核心,或者两者都很重要的东西。
发布于 2019-12-20 08:57:59
IInvocation.Method属性的类型是System.Reflection.MethodInfo,用JSON.NET序列化对象的JSON与Castle无关。
查看.NET框架的序列化功能,可以看到JSON.NET是从哪里获得这些功能的。.NET核心不支持二进制序列化,因此JSON.NET将使用它通常的公共属性序列化,从而导致它遇到循环引用。
[Serializable]
internal class MemberInfoSerializationHolder : ISerializable, IObjectReference
{
//...
public static void GetSerializationInfo(
SerializationInfo info,
String name,
RuntimeType reflectedClass,
String signature,
String signature2,
MemberTypes type,
Type[] genericArguments)
{
if (info == null)
throw new ArgumentNullException("info");
Contract.EndContractBlock();
String assemblyName = reflectedClass.Module.Assembly.FullName;
String typeName = reflectedClass.FullName;
info.SetType(typeof(MemberInfoSerializationHolder));
info.AddValue("Name", name, typeof(String));
info.AddValue("AssemblyName", assemblyName, typeof(String));
info.AddValue("ClassName", typeName, typeof(String));
info.AddValue("Signature", signature, typeof(String));
info.AddValue("Signature2", signature2, typeof(String));
info.AddValue("MemberType", (int)type);
info.AddValue("GenericArguments", genericArguments, typeof(Type[]));
}
//...
}(https://referencesource.microsoft.com/#mscorlib/system/reflection/memberinfoserializationholder.cs)
https://stackoverflow.com/questions/59385822
复制相似问题