using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ReflectToObject.ReflectToStudent();
//Console.ReadLine();
Console.WriteLine();
ReflectToObject.ReflectStudents();
Console.WriteLine();
ReflectToObject.ReflectClass();
Console.WriteLine();
Console.ReadLine();
}
}
class ReflectToObject
{
public static void ReflectToStudent()
{
Student s = new Student();
s.Name = "lilei";
s.Age = 25;
s.Sex = "nv";
Type type = s.GetType();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (PropertyInfo field in properties)
{
Console.WriteLine("属性类型:{0},属性名:{1},属性值:{2}", field.PropertyType, field.Name, field.GetValue(s, null).ToString());
}
}
public static void ReflectStudents()
{
Students c = new Students();
Student sLiLei = new Student();
sLiLei.Name = "liLei";
sLiLei.Age = 20;
sLiLei.Sex = "Femail";
c.LiLei = sLiLei;
Student sHanMeimei = new Student();
sHanMeimei.Name = "HanMeimei";
sHanMeimei.Age = 22;
sHanMeimei.Sex = "Mail";
c.HanMeimei = sHanMeimei;
Type t = c.GetType();
foreach (PropertyInfo field in t.GetProperties())
{
Student s = (Student)field.GetValue(c, null);
foreach (PropertyInfo p in s.GetType().GetProperties())
{
Console.WriteLine("属性类型:{0},属性名:{1},属性值:{2}", p.PropertyType, p.Name, p.GetValue(s, null).ToString());
}
}
}
public static void ReflectClass()
{
Class c = new Class();
List<Student> lS = new List<Student>();
Student sLiLei = new Student();
sLiLei.Name = "liLei";
sLiLei.Age = 20;
sLiLei.Sex = "Femail";
lS.Add(sLiLei);
Student sHanMeimei = new Student();
sHanMeimei.Name = "HanMeimei";
sHanMeimei.Age = 22;
sHanMeimei.Sex = "Mail";
lS.Add(sHanMeimei);
c.Students = lS;
c.ClassName = "计算机科学与技术";
Type t = c.GetType();
foreach (PropertyInfo field in t.GetProperties())
{
if (field.PropertyType == (new List<Student>()).GetType())
{
List<Student> st = (List<Student>)field.GetValue(c, null);
foreach (Student s in st)
{
//Student s = (Student)field.GetValue(c, null);
foreach (PropertyInfo p in s.GetType().GetProperties())
{
Console.WriteLine("属性类型:{0},属性名:{1},属性值:{2}", p.PropertyType, p.Name, p.GetValue(s, null).ToString());
}
}
}
else
{
Console.WriteLine("属性类型:{0},属性名:{1},属性值:{2}", field.PropertyType, field.Name, field.GetValue(c, null).ToString());
}
}
}
}
class Class
{
private string mClassName;
public string ClassName
{
get
{
return mClassName;
}
set
{
mClassName = value;
}
}
private List<Student> mStudents;
public List<Student> Students
{
get
{
return mStudents;
}
set
{
mStudents = value;
}
}
public Class()
{
mStudents = new List<Student>();
}
}
class Students
{
private Student mLiLei;
public Student LiLei
{
get
{
return mLiLei;
}
set
{
mLiLei = value;
}
}
private Student mHanMeimei;
public Student HanMeimei
{
get
{
return mHanMeimei;
}
set
{
mHanMeimei = value;
}
}
public Students()
{
mLiLei = new Student();
mHanMeimei = new Student();
}
}
class Student
{
private string mName = "";
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
}