Example #1
0
    public static string GetDatabaseString(object obj, int identLevel, string name = "", int maxLevel = 6, bool dumpReferencedClassObjects = true)

    {
        if (identLevel > maxLevel)
        {
            return("");
        }
        if (obj == null)
        {
            // return name+" Null";
            return("");
        }
        string db = "";

        if (obj.GetType().IsArray || obj.GetType().IsList())
        {
            if (obj != null)
            {
                object[] arrayObject = ArrayUtility.ConvertToObjectArray(obj);

                for (int ic = 0; ic < arrayObject.Length; ic++)
                {
                    db += "\n" + GetIdentSpaces(identLevel) + ("Element: " + ic + " id: <color=blue>" + arrayObject [ic].GetHashCode() + "</color>");
                    if (!arrayObject [ic].GetType().IsValueType&& arrayObject [ic].GetType() != typeof(string))
                    {
                        db += GetDatabaseString(arrayObject [ic], identLevel + 1, "", maxLevel);
                    }
                    else
                    {
                        db += "\n<color=magenta>" + GetIdentSpaces(identLevel + 1) + arrayObject [ic].ToString() + "</color>";
                    }
                }
            }
        }
        else
        if (!obj.GetType().IsGenericType&& obj.GetType().IsClass)
        {
            FieldInfo[] _PropertyInfos = obj.GetType().GetFields();

            foreach (var fieldInfo in _PropertyInfos)
            {
                var value    = fieldInfo.GetValue(obj);
                var valueStr = value != null ? fieldInfo.FieldType.IsValueType || fieldInfo.FieldType == typeof(string) ? "<color=magenta>" + value.ToString() + "</color>" : "<color=green>" + fieldInfo.FieldType.ToShortString() + "</color> Id: <color=blue>" + value.GetHashCode() + "</color>" : "<color=red>Null</color>";

                db += "\n" + GetIdentSpaces(identLevel) + ((fieldInfo.FieldType.IsClass ? "" : "") + fieldInfo.Name + ": " + valueStr);
                if (value != null && dumpReferencedClassObjects)
                {
                    if (fieldInfo.FieldType.IsClass || obj.GetType().IsArray || (obj.GetType().IsGenericType&& (obj.GetType().GetGenericTypeDefinition() == typeof(List <>))))
                    {
                        db += GetDatabaseString(value, identLevel + 1, "", maxLevel);
                    }
                }
            }
        }
        else
        {
            Debug.Log("Fail: " + obj.GetType());
        }

        return(db);
    }