class Program
{
static void Main(string[] args)
{
var prop = GetpropertyInfoArray();
foreach (var item in prop)
{
Console.WriteLine(item.Name+" "+item.PropertyType+" "+item.GetValue(new A(),null));
}
Console.Read();
}
private static PropertyInfo[] GetpropertyInfoArray()
{
Type type = typeof(A);
object obj = Activator.CreateInstance(type);
return type.GetProperties();
}
}
class A
{
public int E { get; set; } = 123;
public string B { get; set; } = "234234";
public string C { get; set; } = "564645";
public int D { get; set; } = 4234234;
}
结果:
E System.Int32 123
B System.String 234234
C System.String 564645
D System.Int32 4234234
public void PrintInstanceInfor(object t)
{
//获取所有方法
System.Reflection.MethodInfo[] methods = t.GetMethods();
//获取所有成员
System.Reflection.MemberInfo[] members = t.GetMembers();
//获取所有属性
System.Reflection.PropertyInfo[] properties = t.GetProperties();
}