1. 程式人生 > >c# 通過反射輸出成員變量以及成員變量的值

c# 通過反射輸出成員變量以及成員變量的值

ext pub null ctu ref type() 反射 variables bin

/**
* @Author rexzhao
* 工具類 僅限於
* public variable
*/
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UnityEngine;
//#if !UNITY_ANDROID && UNITY_EDITOR

#if __DEBUG
public static class ObjectUtils
{
public static string Print_VariablesOf<T>(T t)

{
var type = t.GetType();
var Fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
StringBuilder sb = new StringBuilder();
foreach (var finfo in Fields)
{
var test = finfo.GetValue(t);
if (test == null)
continue;
sb.Append(finfo.Name.ToString());
sb.Append(": ");
sb.Append(test.ToString());
sb.AppendLine();
}
return sb.ToString();
}
}
#endif

c# 通過反射輸出成員變量以及成員變量的值