.NET 获取类型中的属性

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

解决方案

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

引用命名空间

using System.Reflection; 
  • 1.

实体类

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

获取方法

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

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

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

2009-07-22 17:55:52

2009-08-04 17:30:23

cookieless属ASP.NET

2009-11-12 13:19:55

2009-12-04 09:14:05

.NET 4.0

2024-11-27 00:24:04

2011-04-13 14:37:35

十进制浮点.NET

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对象的属

2009-10-10 09:53:07

.NET值类型

2010-05-06 17:46:47

2009-07-29 09:34:54

IsPostBack属ASP.NET

2023-11-20 14:41:34

Python属性

2023-12-01 10:20:04

Python类属性

2009-10-10 16:40:37

VB.NET Cont
点赞
收藏

51CTO技术栈公众号