89 lines
2.9 KiB
C#
89 lines
2.9 KiB
C#
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Reflection;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
public class Struct : MonoBehaviour
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
public static string StructToString<T>(T structObj, int indentLevel)//StructToString <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><CEBA><EFBFBD><EFBFBD>͵Ľṹ<C4BD>壨<EFBFBD><E5A3A8><EFBFBD><EFBFBD> T<><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD>ֶκ<D6B6><CEBA><EFBFBD><EFBFBD>ԡ<EFBFBD>
|
|||
|
{
|
|||
|
if (structObj == null) return "null";
|
|||
|
|
|||
|
Type type = structObj.GetType();
|
|||
|
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
|
|||
|
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
|||
|
|
|||
|
string indent = new string(' ', indentLevel * 2); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
string result = $"{indent}{type.Name} {{\n";
|
|||
|
|
|||
|
foreach (FieldInfo field in fields)
|
|||
|
{
|
|||
|
object fieldValue = field.GetValue(structObj);
|
|||
|
result += FormatFieldOrProperty(field.Name, fieldValue, indentLevel);
|
|||
|
}
|
|||
|
|
|||
|
foreach (PropertyInfo property in properties)
|
|||
|
{
|
|||
|
if (property.CanRead && property.GetIndexParameters().Length == 0) // ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><D4BF>Զ<EFBFBD>ȡ<EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
object propertyValue = null;
|
|||
|
try
|
|||
|
{
|
|||
|
propertyValue = property.GetValue(structObj);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
propertyValue = $"Error: {ex.Message}";
|
|||
|
}
|
|||
|
result += FormatFieldOrProperty(property.Name, propertyValue, indentLevel);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
result += $"{indent}}}";
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
private static string FormatFieldOrProperty(string name, object value, int indentLevel)
|
|||
|
{
|
|||
|
string indent = new string(' ', indentLevel * 2);
|
|||
|
if (value == null)
|
|||
|
{
|
|||
|
return $"{indent} {name}: null\n";
|
|||
|
}
|
|||
|
|
|||
|
Type valueType = value.GetType();
|
|||
|
if (valueType.IsValueType || valueType == typeof(string))
|
|||
|
{
|
|||
|
return $"{indent} {name}: {value}\n";
|
|||
|
}
|
|||
|
else if (value is IDictionary dictionary)
|
|||
|
{
|
|||
|
string dictResult = $"{indent} {name}: {{\n";
|
|||
|
foreach (var key in dictionary.Keys)
|
|||
|
{
|
|||
|
var keyValue = dictionary[key];
|
|||
|
dictResult += $"{indent} {key}: {StructToString(keyValue, indentLevel + 2)}\n";
|
|||
|
}
|
|||
|
dictResult += $"{indent} }}\n";
|
|||
|
return dictResult;
|
|||
|
}
|
|||
|
else if (value is IEnumerable enumerable && !(value is string))
|
|||
|
{
|
|||
|
string listResult = $"{indent} {name}: [\n";
|
|||
|
foreach (var item in enumerable)
|
|||
|
{
|
|||
|
listResult += $"{indent} {StructToString(item, indentLevel + 2)}\n";
|
|||
|
}
|
|||
|
listResult += $"{indent} ]\n";
|
|||
|
return listResult;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return $"{indent} {name}: {StructToString(value, indentLevel + 1)}\n";
|
|||
|
}
|
|||
|
}
|
|||
|
}
|