/// <summary>
   /// 反射赋值
   /// </summary>
   public class ObjectReflection
   {
       public static PropertyInfo[] GetPropertyInfos(Type type)
       {
           return type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
       }
       /// <summary>
       /// 实体属性反射
       /// </summary>
       /// <typeparam name="S">赋值对象</typeparam>
       /// <typeparam name="T">被赋值对象</typeparam>
       /// <param name="s"></param>
       /// <param name="t"></param>
       public static void AutoMapping<S, T>(S s, T t)
       {
           PropertyInfo[] pps = GetPropertyInfos(s.GetType());
           Type target = t.GetType();

foreach (var pp in pps)
           {
               PropertyInfo targetPP = target.GetProperty(pp.Name);
               object value = pp.GetValue(s, null);

if (targetPP != null && value != null)
               {
                   targetPP.SetValue(t, value, null);
               }
           }
       }
   }
用法  ObjectReflection.AutoMapping(model, vmModel);

这里将model属性的值赋值给了具体相同属性名称的vmModel。

C# 通过反射为一个对象赋值的更多相关文章

  1. C# 反射给对象赋值遇到的问题——类型转换

    反射给对象赋值遇到的问题——类型转换 给一个对象属性赋值可以通过PropertyInfo.SetValue()方式进行赋值,但要注意值的类型要与属性保持一致.    创建对象实例的两种方法: 1. 1 ...

  2. 反射给对象赋值遇到的问题——类型转换[转http://blog.csdn.net/xiaohan2826/article/details/8536074]

    发布时间:2012-10-25 10:49浏览次数:225 给一个对象属性赋值可以通过PropertyInfo.SetValue()方式进行赋值,但要注意值的类型要与属性保持一致.   创建对象实例的 ...

  3. c# net 使用反射为对象赋值

    public T Bson2T(MongoDB.Bson.BsonDocument bson) { T t = default(T); //获取T类中的所有属性 PropertyInfo[] Tpro ...

  4. js 把一个对象赋值给另一个对象会指向同一个内存地址

    先看一段代码: var arr1 = [1,2,3]; var arr2 = arr1; arr2.push(4); console.log(arr1)//[1,2,3,4] 为什么会输出 的是[1, ...

  5. C#中使用反射遍历一个对象属性和值以及百分数

    对某个类的实例化对象, 遍历获取所有属性(子成员)的方法(采用反射): using (var context = new YZS_TRAEntities()) { ).FirstOrDefault() ...

  6. Web 使用反射获得一个对象的所有get方法

    问题描述: 由于想知道request中包含哪些getter方法,就想通过反射进行遍历,然后输出,结果异常,异常信息: 问题代码: try { outGetter(request); } catch ( ...

  7. C#通过反射给对象赋值

    class Program { static void Main(string[] args) { UserSearchRequest model = new UserSearchRequest() ...

  8. JavaScript 反射和属性赋值!

    function Antzone(){ this.webName="蚂蚁部落"; this.age=6; } Antzone.prototype={ address:"青 ...

  9. Java 使用反射给属性赋值

    package com.nf147.manage.spring; import java.lang.reflect.Field; public class Cat { private String n ...

随机推荐

  1. 使用Pods和自定义静态库实现多工程联编

    使用Pods和自定义静态库实现多工程联编 字数607 阅读112 评论0 喜欢0 近来随着公司项目开发的深入,项目的规范也就越来越高了,为了更加方便的管理自定义静态库与pods之间的联系,好好的研究了 ...

  2. Flume 读取JMS 消息队列消息,并将消息写入HDFS

    利用Apache Flume 读取JMS 消息队列消息.并将消息写入HDFS,flume agent配置例如以下: flume-agent.conf #name the  components on ...

  3. System.IO.Path

    System.IO.Path 分类: C#2011-03-23 10:54 1073人阅读 评论(0) 收藏 举报 扩展磁盘string2010c System.IO.Path提供了一些处理文件名和路 ...

  4. C# 实现DataGridView分页功能

    C#实现DataGridView分页功能 2010-07-17 13:45:42|  分类: C#|字号 订阅     从界面可以看到,在设计时需要一个DataGridView.BindingNavi ...

  5. Wall(凸包)

    http://poj.org/problem?id=1113 题意:给出一些点的坐标,和一个半径r,求出这些点围成的凸包的周长再加上一个半径为r的圆的周长. #include <stdio.h& ...

  6. Hardwood Species(map)

    http://poj.org/problem?id=2418 题意:给定一系列字符串,要求按字典序升序输出每个串,并输出每个串出现的百分比. 用map做的,交c++A了,G++ WA..so sad. ...

  7. 0505 php-数组、控制语句、函数

    数 组 (定义.使用.赋值.遍历.分类.冒泡排序) 1.数组包括元素.下标.数组长度 2.php中的数组长度用$len = count("$数组名"); 3.定义一个数组:$arr ...

  8. [Apple开发者帐户帮助]四、管理密钥(1)创建私钥以访问服务

    私钥允许您访问和验证与某些应用服务(如APN,MusicKit和DeviceCheck)的通信.您将在对该服务的请求中使用JSON Web令牌(JWT)中的私钥. 所需角色:帐户持有人或管理员. 在“ ...

  9. mysql 年龄计算(根据生日字段)

    mysql 年龄计算(根据生日字段) year( from_days( datediff( now( ), birthdate))) //获取年龄 now() 当前时间,精确到秒 datediff(b ...

  10. Struts和Spring MVC的比较(非原创)

    文章大纲 一.Spring MVC项目例子二.Struts项目例子三.Struts和Spring MVC对比四.参考文章   一.Spring MVC项目例子 https://www.jianshu. ...