zoukankan      html  css  js  c++  java
  • 动态添加实体的值

    public class DynamicBuilder<T>
        {
            private PropertyInfo[] properties;
    
            private DynamicBuilder() { }
    
            public T Build(IDataRecord reader)
            {
                T result = (T)Activator.CreateInstance(typeof(T));
    
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    if (properties[i] != null && !reader.IsDBNull(i))
                    {
                        if (properties[i].PropertyType.IsEnum)
                        {
                            properties[i].SetValue(result, Enum.ToObject(properties[i].PropertyType, reader[i]), null);
                        }
                        else
                        {
                            properties[i].SetValue(result, Convert.ChangeType(reader[i], properties[i].PropertyType), null);
                        }
    
                    }
                }
                return result;
            }
    
            public static DynamicBuilder<T> CreateBuilder(IDataRecord reader)
            {
                DynamicBuilder<T> result = new DynamicBuilder<T>();
                result.properties = new PropertyInfo[reader.FieldCount];
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    result.properties[i] = typeof(T).GetProperty(reader.GetName(i), BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                }
                return result;
            }
        }
  • 相关阅读:
    创建支持SSH服务的镜像
    docker网络基础配置
    docker数据管理
    ELK安装笔记
    OpenVAS虚拟机安装
    nslookup命令
    docker仓库操作
    Percona Monitoring and Management (PMM)安装使用
    zabbix2.4.5安装zatree插件
    docker容器操作
  • 原文地址:https://www.cnblogs.com/z-huan/p/7421285.html
Copyright © 2011-2022 走看看