private fsResult InternalSerialize_1_ProcessCycles(Type storageType, Type overrideConverterType, object instance, out fsData data) { // We have an object definition to serialize. try { // Note that we enter the reference group at the beginning of serialization so that we support // references that are at equal serialization levels, not just nested serialization levels, within // the given subobject. A prime example is serialization a list of references. _references.Enter(); // This type does not need cycle support. // 获得instance.GetType()对应的converter var converter = GetConverter(instance.GetType(), overrideConverterType); // 判断是否需要循环,这里是不循环的情况 if (converter.RequestCycleSupport(instance.GetType()) == false) { return(InternalSerialize_2_Inheritance(storageType, overrideConverterType, instance, out data)); } // 当instance IsClass || IsInterface; 才会执行下面的 // We've already serialized this object instance (or it is pending higher up on the call stack). // Just serialize a reference to it to escape the cycle. // // note: We serialize the int as a string to so that we don't lose any information // in a conversion to/from double. // 使用了引用实例的情况,直接返回fsResult.Success if (_references.IsReference(instance)) { // 用Dict创建一个fsData, data = fsData.CreateDictionary(); _lazyReferenceWriter.WriteReference(_references.GetReferenceId(instance), data.AsDictionary); return(fsResult.Success); } // 简单理解为记录instance到_references中 // Mark inside the object graph that we've serialized the instance. We do this *before* // serialization so that if we get back into this function recursively, it'll already // be marked and we can handle the cycle properly without going into an infinite loop. _references.MarkSerialized(instance); // We've created the cycle metadata, so we can now serialize the actual object. // InternalSerialize will handle inheritance correctly for us. var result = InternalSerialize_2_Inheritance(storageType, overrideConverterType, instance, out data); if (result.Failed) { return(result); } _lazyReferenceWriter.WriteDefinition(_references.GetReferenceId(instance), data); return(result); } finally { if (_references.Exit()) { _lazyReferenceWriter.Clear(); } } }
//... fsResult Internal_Serialize(Type storageType, object instance, out fsData data, Type overrideConverterType) { var instanceType = instance.GetType(); var instanceTypeConverter = GetConverter(instanceType, overrideConverterType); if (instanceTypeConverter == null) { data = new fsData(); // return fsResult.Warn(string.Format("No converter for {0}", instanceType)); return(fsResult.Success); } var needsCycleSupport = instanceType.RTIsDefined <fsSerializeAsReference>(true); if (needsCycleSupport) { // We've already serialized this object instance (or it is pending higher up on the call stack). // Just serialize a reference to it to escape the cycle. if (_references.IsReference(instance)) { data = fsData.CreateDictionary(); _lazyReferenceWriter.WriteReference(_references.GetReferenceId(instance), data.AsDictionary); return(fsResult.Success); } // Mark inside the object graph that we've serialized the instance. We do this *before* // serialization so that if we get back into this function recursively, it'll already // be marked and we can handle the cycle properly without going into an infinite loop. _references.MarkSerialized(instance); } //push collector TryPush(instance); // Serialize the instance with it's actual instance type, not storageType. var serializeResult = instanceTypeConverter.TrySerialize(instance, out data, instanceType); //pop collector TryPop(instance); if (serializeResult.Failed) { return(serializeResult); } // Do we need to add type information? If the field type and the instance type are different. if (storageType != instanceType && GetConverter(storageType, overrideConverterType).RequestInheritanceSupport(storageType)) { EnsureDictionary(ref data); data.AsDictionary[KEY_INSTANCE_TYPE] = new fsData(instanceType.FullName); } if (needsCycleSupport) { _lazyReferenceWriter.WriteDefinition(_references.GetReferenceId(instance), data); } return(serializeResult); }