c# 不同类同属性赋值

private D Mapper<D, S>(S s)
        {
            D d = Activator.CreateInstance<D>();
            try
            {
                var sType = s.GetType();
                var dType = typeof(D);
                foreach (PropertyInfo sP in sType.GetProperties())
                {
                    foreach (PropertyInfo dP in dType.GetProperties())
                    {
                        if (dP.Name == sP.Name)
                        {
                            dP.SetValue(d, sP.GetValue(s)); break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return d;
        }

 

posted @ 2022-07-13 09:18  vba是最好的语言  阅读(186)  评论(0编辑  收藏  举报