2024年1月25日发(作者:)

c#遍历对象属性给对象赋值using System;using c;using ;using ;using ;using trols;

namespace WebApplication33{ public partial class Default : { protected void Page_Load(object sender, EventArgs e) { Values item = SetValues(); PrintValues(item); } ///

/// 通过遍历属性输出属性和值 /// /// private void PrintValues(Values item) { tyInfo[] properties = e().GetProperties(); foreach (tyInfo property in properties) { string name=; string value = ue(item).ToString(); } } /// /// 通过遍历属性赋值 /// /// private Values SetValues() { Values item = new Values(); tyInfo[] properties = e().GetProperties(); for (int i = 0; i < ; i++) { properties[i].SetValue(item, (i + 1)); } return item; } } class Values { public int Value1 { get; set; } public int Value2 { get; set; } public int Value3 { get; set; } public int Value4 { get; set; } public int Value5 { get; set; } public int Value6 { get; set; } public int Value7 { get; set; }

public int Value8 { get; set; } public int Value9 { get; set; } public int Value10 { get; set; } }}————————————————public class PP 2 { 3 public string a { get; set; } 4 public string b { get; set; } 5 public string c { get; set; } 6 } 7 class Program 8 { 9 static void Main(string[] args)10 {11 Hashtable ht = new Hashtable();12

13

14 ("a", "utf8");15 ("b", "xxxx");16 ("c", "xxxx");17 PP config = new PP();18 PropertyInfo[] propertys = e().GetProperties();19 foreach (PropertyInfo property in propertys)20 {21 for (int i = 0; i < ; i++)22 {23 ue(config, ht[].ToString(), null);24 }25 }26 ine(config.a+"t"+config.b);27 ne();28 }29 }