遍历一个类中的每一个属性、方法、公共字段
//獲取對象類型 ?
? 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
轉載于:https://www.cnblogs.com/dudu837/archive/2010/05/09/1730972.html
總結
以上是生活随笔為你收集整理的遍历一个类中的每一个属性、方法、公共字段的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DataKeyNames
- 下一篇: SuperMap产权登记管理平台