前言
参考文章链接:https://blog.csdn.net/chy_xfn/article/details/82421255
本文内容为,将泛型T或List< T >(包含多层嵌套),转化为特定字符分隔连接的字符串。
一、说明
id:嵌套层级
Symbols1 :该层各字段内,字段名与对应值的连接符 ("" 表示该层不保存字段名)
Symbols2:该层各字段间的分隔符
GetFields与GetProperties可替换,与结构定义相关:
GetFields:public string name;
GetProperties:public string name{get;set;}
二、代码
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using System;
public class UniversalTToString : MonoBehaviour {
//定义公有变量,提供全局访问;
public static readonly UniversalTToString instance = new UniversalTToString ();
private string[] Symbols1 = new string[] { };
private string[] Symbols2 = new string[] { };
public string Return_StringByT<T>(T informs, int id = 0)//解析泛型
{
string result = null;
if (informs.GetType().Name == typeof(List<>).Name || informs.GetType().Name == typeof(Queue<>).Name)//泛型是List
{
result = result + Return_Informs(informs, id);
}
else
{
result = result + Return_Inform(informs, id);//泛型不是List
}
return result;
}
private string Return_Informs<T>(T informs, int id)//处理List
{
string result = null;
string symbol = Symbols2[id];
object subObj = informs;
int count = Convert.ToInt32(subObj.GetType().GetProperty("Count").GetValue(subObj, null));
if (count > 0)
{
for (int i = 0; i < count; i++)
{
object item = subObj.GetType().GetProperty("Item").GetValue(subObj, new object[] { i });
result = result + Return_StringByT(item, id + 1) + symbol;//判断该项是否是List
}
result = result.Substring(0, result.Length - symbol.Length);
}
else
{
result = "Null";
}
return result;
}
private string Return_Inform<T>(T informs, int id)//处理非List
{
string result = null;
Type type = informs.GetType();
FieldInfo[] fields = type.GetFields();
if (fields.Length == 1)//该项是字段float、int、bool、string
{
result = informs.ToString();
}
else//自定义结构
{
result = Return_Fields(informs, id);
}
return result;
}
private string Return_Fields<T>(T informs, int id)//处理自定义结构
{
string result = null;
string symbol1 = Symbols1[id];
string symbol2 = Symbols2[id];
Type type = informs.GetType();
FieldInfo[] fields = type.GetFields();
if (symbol1 == "")
{
foreach (FieldInfo f in fields)
{
if (f.FieldType.IsGenericType)
{
object subObj = f.GetValue(informs);
result = result + Return_StringByT(subObj, id + 1) + symbol2;
}
else
{
result = result + Return_Field(f.GetValue(informs), id + 1) + symbol2;
}
}
}
else
{
foreach (FieldInfo f in fields)
{
if (f.FieldType.IsGenericType)
{
object subObj = f.GetValue(informs);
result = result + f.Name + symbol1 + Return_StringByT(subObj, id + 1) + symbol2;
}
else
{
result = result + f.Name + symbol1 + Return_Field(f.GetValue(informs), id + 1) + symbol2;
}
}
}
result = result.Substring(0, result.Length - symbol2.Length);
return result;
}
private string Return_Field<T>(T value, int id)//处理字段
{
if(value == null)
{
return null;
}
string result = null;
if (value.GetType() != typeof(string)
&& value.GetType() != typeof(int)
&& value.GetType() != typeof(float)
&& value.GetType() != typeof(bool))//是自定义结构体
{
result = Return_Inform(value, id);
}
else
{
result = value.ToString();
}
return result;
}
}