using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Text; public class RefPrint { public static PrintLog PrintTool; private static StringBuilder sb = new StringBuilder(); public static void PrintClassInfo<T>(T obj) where T : class { if (obj == null) { PrintTool.WriteLine("print class of null"); return; } sb = new StringBuilder(); sb = sb.AppendLine(obj.GetType().Name); Type t = obj.GetType(); PrintFields(t, obj); PrintTool.WriteLine(sb.ToString()); } public static void PrintClassInfo(object obj) { if (obj == null) { PrintTool.WriteLine("print class of null"); return; } sb = new StringBuilder(); sb = sb.AppendLine(obj.GetType().Name); Type t = obj.GetType(); PrintFields(t, obj); PrintTool.WriteLine(sb.ToString()); } public static void PrintList(object listValue) { IEnumerable e = listValue as IEnumerable; FieldInfo[] fList = null; foreach (var c in e) { fList = c.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (var item in fList) { sb = sb.AppendLine(item.FieldType.Name + " " + item.Name + " " + item.GetValue(c).ToString()); } } } public static void PrintAry(object aryValue) { IEnumerable e = aryValue as IEnumerable; FieldInfo[] fList = null; foreach (var c in e) { if (c.GetType().Name.Contains("String")) { sb = sb.AppendLine(c.GetType().Name + " " + c); } else if (!c.GetType().Name.Contains("String") && c.GetType().IsClass) { fList = c.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (var item in fList) { sb = sb.AppendLine(item.FieldType.Name + " " + item.Name + " " + item.GetValue(c)); } } else { sb.AppendLine(c.GetType().Name + " " + c + " "); } } } public static void PrintMemebers(Type t, object value) { var mList = t.GetMembers(); foreach (var item in mList) { sb = sb.AppendLine("method: " + item.Name); } } public static void PrintFields(Type t, object value) { var fList = t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (var item in fList) { if (!item.FieldType.IsClass || (item.FieldType.Name == "String")) sb = sb.AppendLine(item.FieldType.Name + " " + item.Name + " " + item.GetValue(value)); if (item.FieldType.Name.Contains("List")) { sb = sb.AppendLine("集合" + item.Name + ":"); Object listValue = item.GetValue(value); PrintList(listValue); } if (item.FieldType.Name.Contains("[]")) { sb = sb.AppendLine("数组" + item.Name + ":"); Object aryValue = item.GetValue(value); PrintAry(aryValue); } } } public static void PrintProperties(Type t, object value) { var pList = t.GetProperties(); foreach (var item in pList) { sb = sb.AppendLine(item.Name + " " + item.GetValue(value, new object[] { })); } } } public interface PrintLog { void WriteLine(string text); }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; using System.Collections; namespace 反射查看类的信息 { class Program { static void Main(string[] args) { PlateFace f = new PlateFace(); f.Add(new PlateFaceSon()); RefPrint.PrintTool = new MyPrint(); RefPrint.PrintClassInfo<PlateFace>(f); Console.ReadLine(); } } public class MyPrint : PrintLog { public void WriteLine(string text) { Console.WriteLine(text); } } public class PlateFace { private int a = 1; private double b = 2; private string c = "c"; private int[] d = { 1, 2, 3, 4 }; private string[] e = { "A", "b", "c", "1" }; private List<PlateFaceSon> f = new List<PlateFaceSon>(); private Dictionary<int, PlateFaceSon> j = new Dictionary<int, PlateFaceSon>(); public void Test() { } public void Add(PlateFaceSon t) { f.Add(t); } } public class PlateFaceSon { public string Name = "盘子脸的儿子"; public int Age = 10; public override string ToString() { return "Name: " + Name + "Age: " + Age; } } }