zoukankan      html  css  js  c++  java
  • 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
        {
        }
    }
  • 相关阅读:
    多项式乘法
    容斥计算多重组合
    D. Tokitsukaze, CSL and Stone Game
    优惠买商品(dp、greedy)
    数星星(单点更新,求前缀和)
    信息推送(单点更新,求前缀和)
    互相送礼物
    Codeforces Round #611 (Div. 3)E. New Year Parties
    多源bfs
    mysql事务和锁
  • 原文地址:https://www.cnblogs.com/jacketlin/p/12469636.html
Copyright © 2011-2022 走看看