博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# 遍历对象属性给对象赋值
阅读量:4302 次
发布时间:2019-05-27

本文共 2210 字,大约阅读时间需要 7 分钟。

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication33
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Values item = SetValues();
            PrintValues(item);
        }
        /// <summary>
        /// 通过遍历属性输出属性和值
        /// </summary>
        /// <param name="item"></param>
        private void PrintValues(Values item)
        {
            System.Reflection.PropertyInfo[] properties = item.GetType().GetProperties();
            foreach (System.Reflection.PropertyInfo property in properties)
            {
                string name=property.Name;
                string value = property.GetValue(item).ToString();
            }
        }
        /// <summary>
        /// 通过遍历属性赋值
        /// </summary>
        /// <returns></returns>
        private Values SetValues()
        {
            Values item = new Values();
            System.Reflection.PropertyInfo[] properties = item.GetType().GetProperties();
            for (int i = 0; i < properties.Length; 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             ht.Add("a", "utf8");15             ht.Add("b", "xxxx");16             ht.Add("c", "xxxx");17             PP config = new PP();18             PropertyInfo[] propertys = config.GetType().GetProperties();19             foreach (PropertyInfo property in propertys)20             {21                 for (int i = 0; i < ht.Count; i++)22                 {23                     property.SetValue(config, ht[property.Name].ToString(), null);24                 }25             }26             Console.WriteLine(config.a+"\t"+config.b);27             Console.ReadLine();28         }29     }

转载地址:http://owlws.baihongyu.com/

你可能感兴趣的文章
jenkins + maven+ gitlab 自动化部署
查看>>
Pull Request流程
查看>>
Lambda 表达式
查看>>
函数式数据处理(一)--流
查看>>
java 流使用
查看>>
java 用流收集数据
查看>>
java并行流
查看>>
CompletableFuture 组合式异步编程
查看>>
mysql查询某一个字段是否包含中文字符
查看>>
Java中equals和==的区别
查看>>
JVM内存管理及GC机制
查看>>
Java:按值传递还是按引用传递详细解说
查看>>
Java中Synchronized的用法
查看>>
阻塞队列
查看>>
linux的基础知识
查看>>
接口技术原理
查看>>
五大串口的基本原理
查看>>
PCB设计技巧与注意事项
查看>>
linux进程之间通讯常用信号
查看>>
main函数带参数
查看>>