zoukankan      html  css  js  c++  java
  • 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对象进行反射,有待完善记录一下

  • 相关阅读:
    C语言I博客作业08
    作业7
    作业6
    作业5
    作业--4
    java基础学习--I/O流
    刷题记录--[CISCN2019 华北赛区 Day2 Web1]Hack World
    ADB测试Android真机
    sqli-labs通关笔记
    Tensorflow入门
  • 原文地址:https://www.cnblogs.com/-Kam/p/9353412.html
Copyright © 2011-2022 走看看