C#通過屬性名字符串獲取、設(shè)置對象屬性值操作示例
本文實(shí)例講述了C#通過屬性名字符串獲取、設(shè)置對象屬性值操作.分享給大家供大家參考,具體如下:
#通過反射獲取對象屬性值并設(shè)置屬性值
0、定義一個(gè)類
public class User { public int Id { get; set; } public string Name { get; set; } public string Age { get; set; } }
1、通過屬性名(字符串)獲取對象屬性值
User u = new User(); u.Name = "lily"; var propName = "Name"; var propNameVal = u.GetType().GetProperty(propName).GetValue(u, null); Console.WriteLine(propNameVal);// "lily"
2、通過屬性名(字符串)設(shè)置對象屬性值
User u = new User(); u.Name = "lily"; var propName = "Name"; var newVal = "MeiMei"; u.GetType().GetProperty(propName).SetValue(u, newVal); Console.WriteLine(propNameVal);// "MeiMei"
#獲取對象的所有屬性名稱及類型
通過類的對象實(shí)現(xiàn)
User u = new User(); foreach (var item in u.GetType().GetProperties()) { Console.WriteLine($"propName:{item.Name},propType:{item.PropertyType.Name}"); } // propName: Id,propType: Int32 // propName:Name,propType: String // propName:Age,propType: String
通過類實(shí)現(xiàn)
foreach (var item in typeof(User).GetProperties()) { Console.WriteLine($"propName:{item.Name},propType:{item.PropertyType.Name}"); } // propName: Id,propType: Int32 // propName:Name,propType: String // propName:Age,propType: String
#判斷對象是否包含某個(gè)屬性
static void Main(string[] args) { User u = new User(); bool isContain= ContainProperty(u,"Name");// true } public static bool ContainProperty( object instance, string propertyName) { if (instance != null && !string.IsNullOrEmpty(propertyName)) { PropertyInfo _findedPropertyInfo = instance.GetType().GetProperty(propertyName); return (_findedPropertyInfo != null); } return false; }
將其封裝為擴(kuò)展方法
public static class ExtendLibrary { /// <summary> /// 利用反射來判斷對象是否包含某個(gè)屬性 /// </summary> /// <param name="instance">object</param> /// <param name="propertyName">需要判斷的屬性</param> /// <returns>是否包含</returns> public static bool ContainProperty(this object instance, string propertyName) { if (instance != null && !string.IsNullOrEmpty(propertyName)) { PropertyInfo _findedPropertyInfo = instance.GetType().GetProperty(propertyName); return (_findedPropertyInfo != null); } return false; } } static void Main(string[] args) { User u = new User(); bool isContain= u.ContainProperty("Name");// true }
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#遍歷算法與技巧總結(jié)》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
Unity實(shí)現(xiàn)單機(jī)游戲每日簽到系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)單機(jī)游戲每日簽到系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04spreadsheetgear插件屏蔽鼠標(biāo)右鍵的方法
今天用到spreadsheetGear插件,然后右鍵有插件自己的菜單。都是英文的,而且還能打開新的窗體。嵌到程序里面,不太合適,所以著手屏蔽2014-02-02C#使用Json.Net進(jìn)行序列化和反序列化及定制化
在本篇文章里小編給大家分享了關(guān)于C#使用Json.Net進(jìn)行序列化和反序列化及定制化的知識(shí)點(diǎn)總結(jié),需要的朋友們參考學(xué)習(xí)下。2019-05-05WinForm判斷關(guān)閉事件來源于用戶點(diǎn)擊右上角“關(guān)閉”按鈕的方法
這篇文章主要介紹了WinForm判斷關(guān)閉事件來源于用戶點(diǎn)擊右上角“關(guān)閉”按鈕的方法,涉及C#針對WinForm事件的判定技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09C#創(chuàng)建壓縮文件的實(shí)現(xiàn)代碼
本篇文章主要介紹了C# 創(chuàng)建壓縮文件的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05C#、ASP.NET通用擴(kuò)展工具類之LogicSugar
這篇文章主要介紹了C#、ASP.NET通用擴(kuò)展工具類之LogicSugar,本文直接給出實(shí)現(xiàn)代碼和使用方法示例,需要的朋友可以參考下2015-06-06C# 漢字轉(zhuǎn)拼音實(shí)例(支持GB2312字符集中所有漢字)
本篇文章主要介紹了C# 漢字轉(zhuǎn)拼音實(shí)例(支持GB2312字符集中所有漢字) ,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12