Object 反射 List
public static object DeserializeModel<T>(object dataModel)
{
Type type = typeof(T);
Object entity = Activator.CreateInstance(type);
Type data = dataModel.GetType();
if (data.Name.Contains("List"))
{
List<T> listCall = new List<T>();
//需要被转换的对象
IEnumerable<object> list = dataModel as IEnumerable<object>;
foreach (var sourceModel in list)
{
entity = Activator.CreateInstance(type);
//循环需要被反对对象的字段
foreach (var item in type.GetProperties())
{
var source = sourceModel.GetType();
if (source.GetProperty(item.Name) != null)
{
var val = source.GetProperty(item.Name).GetValue(sourceModel, null);
item.SetValue(entity, val);
} }
listCall.Add((T)entity);
}
return listCall; }
else
{
foreach (var item in type.GetProperties())
{
if (data.GetProperty(item.Name) != null)
{
var val = data.GetProperty(item.Name).GetValue(dataModel, null);
item.SetValue(entity, val);
} }
return (T)entity;
}
}
根据T类型将传入进来的Object对象进行反射,有待完善记录一下
Object 反射 List的更多相关文章
- Scala对class/object反射
近期有需求,要根据解析字符串,根据字符串的内容去调用方法.想到的基本就是使用反射.但是基本上能找到的资料,全没有讲scala的反射.只有零星点点的讲解.大部分都是用scala的语法写java反射最后翻 ...
- day26——tyoe元类与object的联系、反射、函数与方法的区别、双下方法
day26 type元类与object联系 type 获取对象从属于的类 python 中一切皆对象, 类在某种意义上也是一个对象,python中自己定义的类,以及大部分内置类,都是由type元类(构 ...
- C#基础系列——反射笔记
前言:使用反射也有几年了,但是一直觉得,反这个概念很抽象,今天有时间就来总结下这个知识点. 1.为什么需要反射: 最初使用反射的时候,作为小菜总是不理解,既然可以通过new 一个对象的方式得到对象,然 ...
- C#的反射机制
using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...
- C# 反射相关的东西
public class PlugingManager { //插件装载器 public ArrayList Plugins = new ArrayList(); ...
- 在C++中反射调用.NET(三)
在.NET与C++之间传输集合数据 上一篇<在C++中反射调用.NET(二)>中,我们尝试了反射调用一个返回DTO对象的.NET方法,今天来看看如何在.NET与C++之间传输集合数据. 使 ...
- Go语言反射规则
Go语言反射规则 - The Laws of Reflection 转:http://my.oschina.net/qbit/blog/213720 原文地址:http://blog.golang.o ...
- java的反射应用
class B{ public static void main(String[] arg){ Class c_a = Class.forName(packageName + "." ...
- 23 The Laws of Reflection 反射定律:反射包的基本原理
The Laws of Reflection 反射定律:反射包的基本原理 6 September 2011 Introduction 介绍 Reflection in computing is th ...
随机推荐
- 免费馅饼 HDU - 1176 基础dp
/*题都是有一个状态转移方程式 , 只要推出方程式就问题不大了,首 先对于gameboy来说他下一秒只能 在0~10这十一个位置移动, 而对于1~9这九个位置来说他可以移动(假设他现在的位置为x)到x ...
- 《深入理解java虚拟机》读书笔记六——第七章
第七章 虚拟机类加载机制 1.类加载的时机 虚拟机的类加载机制: 虚拟机把描述类的数据从class文件中加载到内存,并对数据进行校验.转换解析和初始化,最终形成了可以被虚拟机直接使用的Java类型,这 ...
- Dataset: online data
From Kaggle: Appliances Energy Prediction Energy consumption of the Netherlands International Energy ...
- navicat异常 - 1130-host ... is not allowed to connect to this MySql server
错误描述 用navicat连接数据库报错:1130-host ... is not allowed to connect to this MySql server如何处理 解决方案 1.连接服务器: ...
- AntDesign(React)学习-15 组件定义、connect、interface
虽然常用的编码用一种即可,但是看别人文档或者示例时,有的写法不熟悉的话看着很不习惯,整理几种实现同一功能的不同写法 1.Dva Connect与@Connect import React, { Pro ...
- vscode与MinGW64调试c++报错
这个问题在刚配好环境测试的时候往往不会被发现,因为单纯的c++编译调试是没问题的.但一旦调试使用stl库的代码就会报错,而编译又没问题且可以正常运行,但在vscode的集成终端里运行不会显示任何本该显 ...
- IDEA 在SVN上更新代码错误: Error:Server SSL certificate rejected
在IDEA中更新代码到SVN中 ,出现了 Error:Server SSL certificate rejected ---服务器的SSL证书 的错误 之前在网上有找过一些相关的做法,但是 ...
- R parallel包学习笔记2
这个部分我在datacamp上面学习笔记,可视化的性能很差,使用的函数也很少. 可以参考一下大佬的博客园个人感觉他们讲的真的很详细 https://cosx.org/2016/09/r-and-par ...
- go语言 实现对称加密解密算法
package main import ( "bytes" "crypto/aes" "crypto/cipher" "crypt ...
- winform常用控件介绍
1.窗体 12.Label 控件 33.TextBox 控件 44.RichTextBox控件 55.NumericUpDown 控件 76.Button 控件 77.GroupBox 控件 78.R ...