首页 > 编程语言 >c#将一个类型对象数据赋值到另一个类型对象(名字相同的情况)

c#将一个类型对象数据赋值到另一个类型对象(名字相同的情况)

时间:2024-04-28 15:35:38浏览次数:17  
标签:dto Reflection c# GetType System entity 对象 GetProperties 类型

 /// <summary>
 /// 将一个类型对象数据赋值到另一个类型对象(名字相同的情况)
 /// </summary>
 /// <typeparam name="T">目标类型</typeparam>
 /// <param name="entity">目标类型对象</param>
 /// <param name="dto">源对象</param>
 /// <returns></returns>
 public static object EntityDataEntity<T>(T entity, object dto) where T:class,new()
 {
     //if (dto == null || entity == null){return entity;}

     //System.Reflection.PropertyInfo[] entityProperties = entity.GetType().GetProperties(System.Reflection.BindingFlags.Public);
     //System.Reflection.PropertyInfo[] dtoProperties = dto.GetType().GetProperties(System.Reflection.BindingFlags.Public);

     System.Reflection.PropertyInfo[] entityProperties = entity.GetType().GetProperties();
     System.Reflection.PropertyInfo[] dtoProperties = dto.GetType().GetProperties();

     if (entityProperties.Length<=0){return entity;}
     if (dtoProperties.Length <= 0){return entity;}

     foreach (System.Reflection.PropertyInfo item in entityProperties) {
         foreach (var dtoItem in dtoProperties)
         {
             if (item.Name == dtoItem.Name)
             {
                 if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
                 {
                     object value = dtoItem.GetValue(dto, null);
                     if (value != null)
                         item.SetValue(entity, value, null);
                     break;
                 }
                 else { 
                     object value = item.GetValue(entity, null);
                     object dtoValue=dtoItem.GetValue(dto, null);
                     value = EntityDataEntity(value, dtoValue);
                     if (value != null)
                         item.SetValue(entity, value, null);
                     break;
                 }
             }
         }
     }
     return entity;
 }

  

标签:dto,Reflection,c#,GetType,System,entity,对象,GetProperties,类型
From: https://www.cnblogs.com/chengeng/p/18163825

相关文章

  • Asp.Net.Core -Authentication认证
     认证流程:1.在Startup类中的ConfigureServices方法通过添加AddAuthentication注册我们最主要的三个对象AuthenticationService,AuthenticationHandlerProvider,AuthenticationSchemeProvider2.通过AddAuthentication返回的AuthenticationBuilder通过AddJwtBearer(或者AddCook......
  • IfcDerivedUnit
    IfcDerivedUnit实体定义注:定义依据ISO/CD10303-41:1992派生单位是单位的表达式。示例牛顿每平方毫米是一个推导单位。注:对应的ISO10303名称:derived_unit,正式标准的最终定义请参考ISO/IS10303-41。IFC1.5.1中的新实体。 Attributeinheritance#AttributeTypeCardinalityD......
  • SecureCRT常用技巧
    鼠标复制:(常用)options ->globaloptions-> Terminal 钩上Copyonselect,并钩上pasteon【right】button这样在SecureCRT中用鼠标选中一段字符,就可以直接复制到剪切板,按鼠标右键完成粘贴。双击复制并打开新session:(常用)options->globaloptions->Terminal->Tabs......
  • RocketMQ生产者启动源码
    核心代码初始化Default生产者DefaultMQProducerproducer=newDefaultMQProducer(PRODUCER_GROUP);设置NameAddr地址producer.setNamesrvAddr(DEFAULT_NAMESRVADDR);producer.start();分析newDefaultMQProducer(PRODUCER_GROUP)publicDefaultMQProducer(finalStringp......
  • docker 安装 Oracle_12c
    最近做一个功能,需要连接:mysql、mssql、oracle,开发语言是nodejs,框架是nestjs框架,orm使用的typeorm,package包括:"oracledb":"^6.4.0""mssql":"^10.0.2""mysql2":"^3.9.7"公司没有mysql及oracle数据库,所以我在自己的云上用docker安装下,测试下。mysql还行,但是......
  • AtCoder Beginner Contest 351 E - Jump Distance Sum 切比雪夫距离与曼哈顿距离的转
    先说知识点。曼哈顿距离:横纵坐标距离差的绝对值的和,即|X1-X2|+|Y1-Y2|,离(0,0)点曼哈顿距离为1的点形成的是一个旋转45度后的正方形切比雪夫距离:横纵坐标距离差的绝对值的最大值,即max(|X1-X2|,|Y1-Y2|),离(0,0)点切比雪夫距离为1的点形成的是一个不旋转的正方形曼哈......
  • [HNCTF 2022 WEEK4]ezheap
    [HNCTF2022WEEK4]ezheapOff-By-One|堆溢出|leak_libc[*]'/home/bamuwe/ezheap/ezheap'Arch:amd64-64-littleRELRO:FullRELROStack:CanaryfoundNX:NXenabledPIE:PIEenabled$checksec./ezheapEasyNo......
  • Docker安装笔记
    1、配置yum仓库(系统为Centos7)创建Centos基础镜像仓库,到阿里云镜像站https://developer.aliyun.com/mirror/找到Centos可以找到对应系统的镜像仓库。wget-O/etc/yum.repos.d/CentOS-Base.repohttps://mirrors.aliyun.com/repo/Centos-7.repo2、配置docker仓库创建Docker镜......
  • asp.net core 多个授权策略选择单个策略
    首先假设我们依据官方示例有这样一个自定义的授权handlerpublicclassFunAuthorizeAttribute:AuthorizeAttribute,IAuthorizationRequirement,IAuthorizationRequirementData{publicFunAuthorizeAttribute():this(null,true){}publicFun......
  • flutter项目安装nfc_manager后项目运行不起来
    安装nfc_manager后运行一直卡着,生成不了APPAndroidManifest.xml中的已经添加了的权限,插件版本也是对的上的后面进行卸载→重装进行测试、在安装插件时看到有这样一段提示PleaseenableDeveloperModeinyoursystemsettings.Run startms-settings:developers toopense......