这段时间无聊,N年没有更新了,本来我很早就准备更新了(课题是讲CLR via C#)但是一直都很懒没有把接下的总结做下来,所以一直都没发布。
今日发布是为了自己不要在偷懒不去总结些东西,所以逼着自己写下来一些东西,废话到此为止。
------------------------------------------------------------------------------------------------------------------------------------------------------------------
在上次我介绍过动态反射,这次介绍通过继承抽象类和反射来实现动态实体类获得实体类属性值。
首先创建解决方案(ReflexProperty名称)
创建3个文件(EntityModels.cs,GetEntityValue.cs,Person.cs)。
然后来分析这4个文件主要用途
EntityModels.cs是一个抽象实体类,主要用于给后面的实体类继承实现。(在这里插入一下段解释,我这个是为了不改变程序结构,只是添加实体类继承抽象类和添加表就可以实现对数据的管理)在
后面我来慢慢介绍和解析程序结构。
GetEntityValue.cs用于管理反射。主要原因是因为GetEntityValue不知道Person类,它这里只是需要带入EntityModels抽象类。就可以获得Person类下面的属性和属性值。
Person.cs是一个继承抽象实体类Person.cs的类。主要是和表字段对应起来。
接下来程序演示下:
WinForm示例
Form1.cs 1 public partial class Form1 : Form
2 {
3 public Form1()
4 {
5 InitializeComponent();
6 }
7 private void button1_Click(object sender, EventArgs e)
8 {
9 Person person = new Person();//创建Person类对象,而Person类继承与EntityModels抽象类
10 person.Name = "martinzhang";
11 person.Age = 1;
12 GetEntityValue entity = new GetEntityValue();
13 ArrayList array_Property = entity.GetValue(person);//参数为EntityModels对象,而Person类是继承与EntityModels抽象类
14 MessageBox.Show(array_Property[0].ToString());
15 MessageBox.Show(array_Property[1].ToString());
16 }
17 }
2 {
3 public Form1()
4 {
5 InitializeComponent();
6 }
7 private void button1_Click(object sender, EventArgs e)
8 {
9 Person person = new Person();//创建Person类对象,而Person类继承与EntityModels抽象类
10 person.Name = "martinzhang";
11 person.Age = 1;
12 GetEntityValue entity = new GetEntityValue();
13 ArrayList array_Property = entity.GetValue(person);//参数为EntityModels对象,而Person类是继承与EntityModels抽象类
14 MessageBox.Show(array_Property[0].ToString());
15 MessageBox.Show(array_Property[1].ToString());
16 }
17 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ReflexProperty
7 {
8 public abstract class EntityModels
9 {
10 //此类属于抽象基类,主要是继承后传入到反射类中使用
11 }
12 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ReflexProperty
7 {
8 public abstract class EntityModels
9 {
10 //此类属于抽象基类,主要是继承后传入到反射类中使用
11 }
12 }
GetEntityValue.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Collections;
6 using System.Reflection;
7
8 namespace ReflexProperty
9 {
10 public class GetEntityValue
11 {
12 //返回一个集合(如果有别的类型,在此也可以通过自己需要来变动)
13 public ArrayList GetValue(EntityModels _entityModels)
14 {
15 Type _type = _entityModels.GetType();//获得抽象类Type类型得到Type类。(注:Type属于反射中一个很重要的类)
16 ArrayList array_Property = new ArrayList();//声明一个ArrayList类,用于接收反射获得值。(不在此多介绍它的用途了)
17 foreach (PropertyInfo _info in _type.GetProperties())//Type类的_type对象中的GetProperties()方法,调用此方法后转换成了属性集合
18 {
19 array_Property.Add(_info.GetValue(_entityModels, null));//返回属性的值。(注:因为Person类继承了抽象类,所以在这里我直接使用抽象类。此处还可以通过Name属性来获得属性名称,如果有问题可以留言。)
20 }
21 return array_Property;//返回类型
22 }
23 }
24 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Collections;
6 using System.Reflection;
7
8 namespace ReflexProperty
9 {
10 public class GetEntityValue
11 {
12 //返回一个集合(如果有别的类型,在此也可以通过自己需要来变动)
13 public ArrayList GetValue(EntityModels _entityModels)
14 {
15 Type _type = _entityModels.GetType();//获得抽象类Type类型得到Type类。(注:Type属于反射中一个很重要的类)
16 ArrayList array_Property = new ArrayList();//声明一个ArrayList类,用于接收反射获得值。(不在此多介绍它的用途了)
17 foreach (PropertyInfo _info in _type.GetProperties())//Type类的_type对象中的GetProperties()方法,调用此方法后转换成了属性集合
18 {
19 array_Property.Add(_info.GetValue(_entityModels, null));//返回属性的值。(注:因为Person类继承了抽象类,所以在这里我直接使用抽象类。此处还可以通过Name属性来获得属性名称,如果有问题可以留言。)
20 }
21 return array_Property;//返回类型
22 }
23 }
24 }