【Unity】泛型转String


前言

参考文章链接: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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值