When I use the TNSerializer to auto serializer custom object, It will have problem with some complex struct.
I debug it and find the problem is the static function: FilterFields, it use the static param: mFieldNames, mFieldValues, this will be overwrite in recursion logic.
I change the FilterFields use new variable, and it work fine now, maybe can help someone.
static void FilterFields(object obj, out List<string> fieldNames, out List<object> fieldValues)
{
Type type = obj.GetType();
List<FieldInfo> fields = type.GetSerializableFields();
fieldNames
= new List
<string>(); fieldValues
= new List
<object>();
for (int i = 0; i < fields.size; ++i)
{
FieldInfo f = fields[i];
object val = f.GetValue(obj);
if (val != null)
{
fieldNames.Add(f.Name);
fieldValues.Add(val);
}
}
}
case 254: // Serialization using Reflection
{
#if REFLECTION_SUPPORT
List<string> fieldNames;
List<object> fieldValues;
FilterFields(obj, out fieldNames, out fieldValues);
bw.WriteInt(fieldNames.size);
for (int i = 0, imax = fieldNames.size; i < imax; ++i)
{
bw.Write(fieldNames[i]);
bw.WriteObject(fieldValues[i]);
}
#else
Debug.LogError("Reflection-based serialization is not supported on this platform.");
#endif
break;
}