这是第一个类,也是我要反射的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
public class Refection
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string age;
public string Age
{
get { return age; }
set { age = value; }
}
}
}
这是主程序的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication4
{
public class Program
{
public static List<object> GetRefection(object obj)
{
List<object> objs = new List<object>();
Type t = obj.GetType();
PropertyInfo[] pros = t.GetProperties();
for (int i = 0; i < 2; i++)
{
foreach (PropertyInfo pro in pros)
{
pro.SetValue(obj, i.ToString(), null);
}
objs.Add(obj);
}
return objs;
}
static void Main(string[] args)
{
Refection refe = new Refection();
List<Object> objs = GetRefection(refe);
for (int i = 0; i < objs.Count; i++)
{
Type t = objs[i].GetType();
PropertyInfo[] pros = t.GetProperties();
foreach (PropertyInfo pro in pros)
{
Console.WriteLine(pro.GetValue(objs[i], null));
}
}
}
}
}
慕森卡
万千封印
相关分类