110,566
社区成员
发帖
与我相关
我的任务
分享
public 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
{
PrintProperties(value);
}
}
}
public 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);
}
}
}
}
//print public properties
public 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)
{
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
Console.WriteLine("{0}:{1}", item.Name, item.GetValue(obj, null));
}
else
{
//我不知道怎样写了
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsApplication26
{
public partial class Form1 : Form
{
class A
{
public string Property1 { get { return "Property1"; } }
public int Property2 { get { return 1; } }
}
class B
{
private A property2=new A();
public string Property1 { get { return "Property1"; } }
public A Property2 { get { return property2; } }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
B b = new B();
foreach (PropertyInfo info in b.GetType().GetProperty("Property2").GetValue(b, null).GetType().GetProperties())
{
MessageBox.Show(string.Format("{0}={1}",info.Name,info.GetValue(b.Property2,null)));
}
}
}
}
B b = new B();
Type t = b.GetType().GetProperty("Property2").GetValue(b, null).GetType();
foreach (PropertyInfo info in b.GetType().GetProperty("Property2").GetValue(b, null).GetType().GetProperties())
{
MessageBox.Show(string.Format("{0}={1}",info.Name,info.GetValue(b.Property2,null)));
}