遍历一个类中的每一个属性、方法、公共字段

//获取对象类型  
  Type   t   =   obj.GetType();  
  //获取类的属性  
  PropertyInfo[]   propertys   =   t.GetProperties();   
  foreach   (System.Reflection.PropertyInfo   pro   in   propertys)  
  {  
  string   fieldName     =   pro.Name;  
  object   fieldValue   =   pro.GetValue(o,null);

Response.Write(fieldName    +fieldValue  +"<br/>");  

}

 //获取类的方法

 foreach(MethodInfo   mif   in   t.GetMethods())  
  {  
  Response.Write(mif.Name+"<br/>");  
  }  

 

//获取类的公共字段:

            FieldInfo[] fields = t.GetFields();
            foreach (FieldInfo fieldInfo in fields)
            {
                ptype.Append("fieldInfo.Name:" + fieldInfo.Name + "<br />");
                ptype.Append("fieldInfo.Value:" + fieldInfo.GetValue(fieldInfo) + "<br />");
            }

 

public class MemCacheKey

    {

        /// <summary>

        /// 全部同城在线人数列表

        /// </summary>

        public const string OnlineCityUserList = "mlg2010OnlineUserList";

        /// <summary>

        ///  全部同城在线人扩展属性(2.0)列表

        /// </summary>

        public const string OnlineCityUserExList = "mlg2010OnlineUserExList";

        /// <summary>

        /// 最新唠叨Top5

        /// </summary>

        public const string NewsChatTop5 = "mlg2010NewsChatTop5";

        /// <summary>

        /// 最新唠叨

        /// </summary>

        public const string NewsChatList = "mlg2010NewsChatList";

        /// <summary>

        /// 最热唠叨

        /// </summary>

        public const string HotChatList = "mlg2010HotChatList";

        /// <summary>

        /// 最新5条日记

        /// </summary>

        public const string NewsTop5DiaryList = "mlg2010NewsDiaryTop5List";

        /// <summary>

        /// 最新日记列表

        /// </summary>

        public const string NewsDiaryList = "mlg2010NewsDiaryList";

        /// <summary>

        /// 最热日记

        /// </summary>

        public const string HotDiaryList = "mlg2010HotDiaryList";

        /// <summary>

        /// 最新照片

        /// </summary>

        public const string NewsPhotoList = "mlg2010NewsPhotoList";

        /// <summary>

        /// 最热照片

        /// </summary>

        public const string HotPhotoList = "mlg2010HotPhotoList";

        /// <summary>

        /// 用户的唠叨

        /// </summary>

        public const string HotChatByPingcoId = "mlg2010HotChatListByPingcoId";

        /// <summary>

        /// 用户的日记列表

        /// </summary>

        public const string HotDiaryByPingcoId = "mlg2010HotDiaryListByPingcoId";

        /// <summary>

        /// 用户照片

        /// </summary>

        public const string HotPhotoByPingcoId = "mlg2010HotPhotoListByPingcoId";

        /// <summary>

        /// 首页栏目分类

        /// </summary>

        public const string MlgIndexSort = "mlg2010IndexSort";

        /// <summary>

        /// 首页信息列表

        /// </summary>

        public const string MlgIndexList = "mlg2010IndexList";



        public const string MlgNewsFeedsTop5List = "mlg2010IndexNewsFeedsTop5List";

        /// <summary>

        /// 信息

        /// </summary>

        public const string MlgNewsInfo = "mlg2010NewsInfo";

        /// <summary>

        /// 按被顶、回复次数

        /// </summary>

        public const string BestChatOrDiaryList = "mlg2010BestChatOrDiaryList";

        /// <summary>

        /// 首页友友秀

        /// </summary>

        public const string IndexYyxString = "mlg2010IndexYyxString";

        /// <summary>

        /// 友友秀

        /// </summary>

        public const string YyxList = "mlg2010YyxList";

        /// <summary>

        /// 新人

        /// </summary>

        public const string NewUserList = "mlg2010NewUserList";

        /// <summary>

        /// 交友广播

        /// </summary>

        public const string NewsNoticeList = "mlg2010NewsNoticeList";

    }

 

--------

输出:

fieldInfo.Name:OnlineCityUserList
fieldInfo.Value:mlg2010OnlineUserList
fieldInfo.Name:OnlineCityUserExList
fieldInfo.Value:mlg2010OnlineUserExList
fieldInfo.Name:NewsChatTop5
fieldInfo.Value:mlg2010NewsChatTop5
fieldInfo.Name:NewsChatList
fieldInfo.Value:mlg2010NewsChatList
fieldInfo.Name:HotChatList
fieldInfo.Value:mlg2010HotChatList
fieldInfo.Name:NewsTop5DiaryList
fieldInfo.Value:mlg2010NewsDiaryTop5List
fieldInfo.Name:NewsDiaryList
fieldInfo.Value:mlg2010NewsDiaryList
fieldInfo.Name:HotDiaryList
fieldInfo.Value:mlg2010HotDiaryList
fieldInfo.Name:NewsPhotoList
fieldInfo.Value:mlg2010NewsPhotoList
fieldInfo.Name:HotPhotoList
fieldInfo.Value:mlg2010HotPhotoList
fieldInfo.Name:HotChatByPingcoId
fieldInfo.Value:mlg2010HotChatListByPingcoId
fieldInfo.Name:HotDiaryByPingcoId
fieldInfo.Value:mlg2010HotDiaryListByPingcoId
fieldInfo.Name:HotPhotoByPingcoId
fieldInfo.Value:mlg2010HotPhotoListByPingcoId
fieldInfo.Name:MlgIndexSort
fieldInfo.Value:mlg2010IndexSort
fieldInfo.Name:MlgIndexList
fieldInfo.Value:mlg2010IndexList
fieldInfo.Name:MlgNewsFeedsTop5List
fieldInfo.Value:mlg2010IndexNewsFeedsTop5List
fieldInfo.Name:MlgNewsInfo
fieldInfo.Value:mlg2010NewsInfo
fieldInfo.Name:BestChatOrDiaryList
fieldInfo.Value:mlg2010BestChatOrDiaryList
fieldInfo.Name:IndexYyxString
fieldInfo.Value:mlg2010IndexYyxString
fieldInfo.Name:YyxList
fieldInfo.Value:mlg2010YyxList
fieldInfo.Name:NewUserList
fieldInfo.Value:mlg2010NewUserList
fieldInfo.Name:NewsNoticeList
fieldInfo.Value:mlg2010NewsNoticeList

你可能感兴趣的:(遍历)