.NET 获取类型中的属性

开发
解决方案:通过反射的方式获取类型中的所有属性。

解决方案

通过反射的方式获取类型中的所有属性。

引用命名空间

  1. using System.Reflection; 

实体类

  1. public class User 
  2.   { 
  3.         private string id; 
  4.         public string Id { get { return id; } set { id = value; } } 
  5.  
  6.         private string name; 
  7.         public string Name { get { return name; } set { name = value; } } 
  8.   } 

获取方法

  1. private PropertyInfo[] GetPropertyInfoArray() 
  2.    { 
  3.         PropertyInfo[] props = null
  4.         try 
  5.         { 
  6.              Type type = typeof(User); 
  7.              object obj = Activator.CreateInstance(type); 
  8.              props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); 
  9.         } 
  10.         catch (Exception ex) 
  11.         { } 
  12.         return props; 
  13.    } 

原文链接:http://www.cnblogs.com/liusuqi/p/3171963.html

责任编辑:陈四芳 来源: M守护神
相关推荐

2009-08-04 17:30:23

cookieless属ASP.NET

2009-07-22 17:55:52

2009-11-12 13:19:55

2011-04-13 14:37:35

十进制浮点.NET

2009-12-04 09:14:05

.NET 4.0

2011-06-08 13:50:39

C#类型转换

2009-07-28 13:17:09

EnableViewSASP.NET

2009-07-23 17:07:58

2009-12-01 09:30:34

ASP.NET MVC

2009-10-26 15:26:37

VB.NET属性

2009-03-02 13:56:29

2011-08-04 09:43:11

ASP.NET控件

2009-07-29 15:07:23

Request对象的属

2010-05-06 17:46:47

2019-12-10 10:31:30

javascriptWeb前端开发

2009-10-10 09:53:07

.NET值类型

2023-11-20 14:41:34

Python属性

2023-12-01 10:20:04

Python类属性

2016-12-20 16:21:18

大数据数据分析

2009-07-29 09:34:54

IsPostBack属ASP.NET
点赞
收藏

51CTO技术栈公众号