Here is code:
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private void Start()
{
var a = new A
{
array = new int[] { 1, 2, 3 }
};
print(JsonConvert.SerializeObject(a));
}
}
/*
*
* Uncomment the comments in <struct A> will cause Infite loop (Unity3D will be hanged)! Why ?
*
*/
[Serializable]
public struct A /*: IEnumerable<int>*/
{
public int[] array;
//public IEnumerator<int> GetEnumerator()
//{
// while (true) foreach (int i in array) yield return i;
//}
//IEnumerator IEnumerable.GetEnumerator()
//{
// while (true) foreach (int i in array) yield return i;
//}
}
If I uncomment the comments in structure A, it will cause Untiy3D hanged forever. I don't know about the workflow of JSON.NET. I don't know why JsonConvert called the method GetEnumerator? I think it should not call. Please help me to understand, Thank you so much