EntityExtend类
Posted jacketlin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EntityExtend类相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp4 { using System; using System.Collections.Generic; using System.Reflection; using System.Text; /// <summary> /// 扩展 /// </summary> public static class EntityExtend { /// <summary> /// 拷贝 /// </summary> /// <param name="entity">实体</param> /// <param name="fromEntity">来源实体</param> public static void CopyFrom(this IEntity entity, IEntity fromEntity) { var thisPros = entity.GetProperties(); var fromPros = fromEntity.GetProperties(); foreach (var key in thisPros.Keys) { if (fromPros.ContainsKey(key)) { var fromProperty = fromPros[key]; var thisProperty = thisPros[key]; if (thisProperty.CanWrite && fromProperty.CanRead && thisProperty.PropertyType == fromProperty.PropertyType) { thisProperty.SetValue(entity, fromProperty.GetValue(fromEntity)); } } } } /// <summary> /// 获取属性键值对 /// </summary> /// <param name="entity">实体</param> /// <returns>键值对</returns> private static IDictionary<string, PropertyInfo> GetProperties(this IEntity entity) { var ret = new Dictionary<string, PropertyInfo>(); PropertyInfo[] properties = entity.GetType().GetProperties(); foreach (var p in properties) { ret.Add(p.Name.ToLower(), p); } return ret; } } public interface IEntity { } }
以上是关于EntityExtend类的主要内容,如果未能解决你的问题,请参考以下文章
elasticsearch代码片段,及工具类SearchEsUtil.java
Android 逆向类加载器 ClassLoader ( 类加载器源码简介 | BaseDexClassLoader | DexClassLoader | PathClassLoader )(代码片段