想起來研究下簡單的反射,然后就在網上找了點資料,也找了點代碼,大致如下:
public static void PRintProperties<T>(T t) { if (t == null) { return; } PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); if (properties.Length <= 0) { return; } foreach (PropertyInfo item in properties) { string name = item.Name; object value = item.GetValue(t, null); if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { Console.WriteLine("{0}:{1}", name, value); } else { foreach (PropertyInfo itemsub in value.GetType().GetProperties()) { PrintProperties(value); } } } }
新聞熱點
疑難解答