博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
反射属性名和属性值
阅读量:7100 次
发布时间:2019-06-28

本文共 2363 字,大约阅读时间需要 7 分钟。

这段时间无聊,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 }

 

EntityModels.cs
 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 }

 

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 }

 

转载于:https://www.cnblogs.com/XiaoLongZhang/archive/2012/11/29/2794581.html

你可能感兴趣的文章
php-memcache
查看>>
MJRefresh tableview基本刷新的封装
查看>>
OpenStack视图
查看>>
有效用例模式阅读笔记一
查看>>
网络爬虫(一)
查看>>
查看Win7系统的cookies
查看>>
JavaScript基础-2
查看>>
python实训第四天
查看>>
5-4-3原则
查看>>
html图像入门
查看>>
C# Mongo Client 2.4.2创建索引
查看>>
我的第四个网页制作:列表标签
查看>>
【python进阶】详解元类及其应用2
查看>>
简单实用的菜单栏
查看>>
AMap行政区查询服务
查看>>
SpringBoot2.0源码分析(一):SpringBoot简单分析
查看>>
LeetCode-96-Unique Binary Search Trees
查看>>
Mac iOS 模拟器录制屏幕生成Gif
查看>>
python练习题
查看>>
oracle log_archive_dest_1 未指定导致flash_recovery_area引发数据库挂起
查看>>