zoukankan      html  css  js  c++  java
  • c# 利用反射动态给实体类对象赋值

    转:http://blog.sina.com.cn/s/blog_659a572b0100xp5s.html

    例子如下


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MvcApplication2.Models;
    using DataAccess;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
    namespace MvcApplication2.Controllers
    {
       /// <summary>
        /// 将datatable装入指定类型的集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class GenericList<T>:List<T>
        {
            public GenericList(DataTable dt, string  f)
            {
                System.Type tt = System.Type.GetType(f);//获取指定名称的类型
                object ff = Activator.CreateInstance(tt, null);//创建指定类型实例
                PropertyInfo[] fields = ff.GetType().GetProperties();//获取指定对象的所有公共属性
                foreach (DataRow dr in dt.Rows)
                {
                    object obj = Activator.CreateInstance(tt, null);
                    foreach (DataColumn dc in dt.Columns)
                    {
                        foreach (PropertyInfo t in fields)
                        {
                            if (dc.ColumnName == t.Name)
                            {
                                t.SetValue(obj, dr[dc.ColumnName], null);//给对象赋值
                                continue;
                            }
                        }

                    }
                    this.Add((T)obj);//将对象填充到list集合
                }
            }
        }


    //////////////////////实体类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    namespace MvcApplication2.Models
    {
        [MetadataType(typeof(EmployeeData))]
        public partial  class Employee
        {

            public class EmployeeData
            {
                [DisplayName("用户ID")]
                public int ID { set; get; }

                [DisplayName("姓名")]
                [Required(ErrorMessage = "用户名不允许为空")]
                [StringLength(50, ErrorMessage = "用户名长度必须小于50个字符")]
                public string Uname { set; get; }

                [DisplayName("性别")]
                public bool Sex { set; get; }

                [DisplayName("出生日期")]
                [Required(ErrorMessage = "出生日期不允许为空")]
                public DateTime Birthday { set; get; }

                [DisplayName("是否婚配")]
                public bool IsHp { set; get; }

              
            }


        }
    }


    //////////////////调用

     GenericList<Employee> list1 = new GenericList<Employee>(dt, "MvcApplication2.Models.Employee");//调用
      return View(list1);

  • 相关阅读:
    iOS- 网络访问JSON数据类型与XML数据类型的实现思路及它们之间的区别
    iOS- AVSpeechSynthesizer——iOS7语音合成器
    iOS- 利用AFNetworking(AFN)
    iOS- 利用AFNetworking(AFN)
    iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
    iOS- 多线程技术的概述及优点
    150. Best Time to Buy and Sell Stock II【medium】
    213. String Compression【easy】
    495. Implement Stack【easy】
    547. Intersection of Two Arrays【easy】
  • 原文地址:https://www.cnblogs.com/jackljf/p/4768616.html
Copyright © 2011-2022 走看看