日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

获取一个对象的属性/属性值,以及动态给属性赋值

發(fā)布時(shí)間:2025/6/17 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 获取一个对象的属性/属性值,以及动态给属性赋值 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//定義類public class MyClass{public int Property1 { get; set; }public string Property2 { get; set; }}       MyClass tmp_Class = new MyClass();tmp_Class.Property1 = 2;tmp_Class.Property2 = "aq23iu";Type type = tmp_Class.GetType(); //獲取類型System.Reflection.PropertyInfo propertyInfo = type.GetProperty("Property1"); //獲取指定名稱的屬性
       //無法指定屬性的名稱時(shí),這樣使用GetProperties()函數(shù) 遍歷 得到每個(gè)屬性的名稱(字符串)
?      ?Type t = tmp_Class.GetType();
??????????? PropertyInfo[] propertyInfoArr = t.GetProperties();
??????????? foreach (PropertyInfo item in propertyInfoArr)
??????????? {
??????????????? PropertyInfo propertyInfo = t.GetProperty(item.Name); //獲取指定名稱的屬性
??????????????? var? value = propertyInfo.GetValue(tmp_Class, null); //獲取屬性值
??????????????? Console.WriteLine(value);
??????????? }
int value_Old = (int)propertyInfo.GetValue(tmp_Class, null); //獲取屬性值 Console.WriteLine(value_Old);propertyInfo.SetValue(tmp_Class, 5, null); //給對(duì)應(yīng)屬性賦值int value_New = (int)propertyInfo.GetValue(tmp_Class, null);Console.WriteLine(value_New);

參考:http://www.bitscn.com/pdb/dotnet/200804/138760.html

轉(zhuǎn)載于:https://www.cnblogs.com/jcdd-4041/p/4184997.html

總結(jié)

以上是生活随笔為你收集整理的获取一个对象的属性/属性值,以及动态给属性赋值的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。