c# 动态反射赋值

public class Form_list
    {
        public BaseData G_no { get; set; }
        public BaseData Cop_g_no { get; set; }
        public BaseData Code_hs { get; set; }
    }

 public class BaseData
    {
        public List<ListOpreateValue> listOpreateValues { get; set; }
        public string Fitvalue { get; set; }
    }

 

public class ListOpreateValue
    {
        public string Value { get; set; }
        public string Opreate { get; set; }
        public string InTime { get; set; }
    }

 

 private async void Cop_g_no_DoubleClick(object sender, EventArgs e)
        {
            ListOpreateValue listOpreateValue = new ListOpreateValue();
            var Tsw = this.TSW.Text.ToString() + "_List_";
            var guid = this.Guid1.Text.ToString();

            listOpreateValue.Value = this.Cop_g_no.Text.ToString();
            listOpreateValue.Opreate = this.Opreater.Text.ToString();
            listOpreateValue.InTime = DateTime.Now.ToString();

            var flag = await ImportDocument.addImportDocument_list(Tsw + guid, "Cop_g_no", listOpreateValue);          

        }

 

  public static async Task<bool> addImportDocument_list(string Key, string DataKey, ListOpreateValue listOpreate)
        {

              Form_list flist = new Form_list();

                #region 基础数据
                BaseData baseData = new BaseData();
                object value;
                //object obj = Activator.CreateInstance(typeof(BaseData));
                #region 添加操作历史
                listOpreateValues.Add(listOpreate);
                baseData.listOpreateValues = listOpreateValues;
                baseData.Fitvalue = listOpreate.Value;
                #endregion
                #endregion

                #region 放入变量

                Type Ts = flist.GetType();

                PropertyInfo[] properties = Ts.GetProperties();

                foreach (PropertyInfo propertyInfo in properties)
                {
                    if (propertyInfo.Name == DataKey)
                    {
                        BaseData obj = new BaseData();
                        PropertyInfo[] properties_ = propertyInfo.PropertyType.GetProperties();
                        foreach (PropertyInfo propertyInfo_ in properties_)
                        {
                            if (propertyInfo_.Name == "Fitvalue")
                            {
                                value = Convert.ChangeType(baseData.Fitvalue, propertyInfo_.PropertyType);
                                propertyInfo_.SetValue(obj, value, null);
                            }
                            else if (propertyInfo_.Name == "listOpreateValues")
                            {
                                value = Convert.ChangeType(baseData.listOpreateValues, propertyInfo_.PropertyType);
                                propertyInfo_.SetValue(obj, value, null);
                            }

                        }
                        value = Convert.ChangeType(obj, propertyInfo.PropertyType);
                        propertyInfo.SetValue(flist, value, null);
                        break;
                    }
                }
                #endregion
            }

 

红色部分代码可以删除, 绿色部分 改为  value = Convert.ChangeType(baseData, propertyInfo.PropertyType);

 

 

修改为:

         

 public static async Task<bool> addImportDocument_list(string Key, string DataKey, ListOpreateValue listOpreate)
        {

            flist = await redis_Op.StringGetAsync<Form_list>(Key);
            if (flist != null)
            {

                PropertyInfo[] properties = typeof(Form_list).GetProperties();
                foreach (PropertyInfo propertyInfo in properties)
                {
                    if (propertyInfo.Name == DataKey)
                    {
                        BaseData baseData = (BaseData)propertyInfo.GetValue(flist, null);
                        if (baseData == null)
                        {
                            #region 基础数据
                            baseData = new BaseData();
                            object value;
                            #region 添加操作历史
                            listOpreateValues.Add(listOpreate);
                            baseData.listOpreateValues = listOpreateValues;
                            baseData.Fitvalue = listOpreate.Value;
                            #endregion
                            #endregion

                            PropertyInfo[] properties_ = propertyInfo.PropertyType.GetProperties();
                            value = Convert.ChangeType(baseData, propertyInfo.PropertyType);
                            propertyInfo.SetValue(flist, value, null);
                        }
                        else
                        {
                           
                            ListOpreateValue = baseData.listOpreateValues.Where(x => x.Opreate == listOpreate.Opreate).FirstOrDefault();
                            if (ListOpreateValue != null)
                            {
                                ListOpreateValue.Value = listOpreate.Value;
                                ListOpreateValue.InTime = listOpreate.InTime;
                            }
                            else
                            {
                                baseData.listOpreateValues.Add(listOpreate);
                            }
                            baseData.Fitvalue = FitValue(baseData.listOpreateValues);
                        }
                        break;
                    }
                }
            }
            else
            {
                flist = new Form_list();

                #region 基础数据
                BaseData baseData = new BaseData();
                object value;
                #region 添加操作历史
                listOpreateValues.Add(listOpreate);
                baseData.listOpreateValues = listOpreateValues;
                baseData.Fitvalue = listOpreate.Value;
                #endregion
                #endregion

                #region 放入变量

                PropertyInfo[] properties = typeof(Form_list).GetProperties();

                foreach (PropertyInfo propertyInfo in properties)
                {
                    if (propertyInfo.Name == DataKey)
                    {
                        PropertyInfo[] properties_ = propertyInfo.PropertyType.GetProperties();
                        value = Convert.ChangeType(baseData, propertyInfo.PropertyType);
                        propertyInfo.SetValue(flist, value, null);
                        break;
                    }
                }
                #endregion
            }

            return await redis_Op.StringSetAsync(Key, flist);
        }

        private static string FitValue(List<ListOpreateValue> listOpreateValues)
        {
            return listOpreateValues.GroupBy(x => x.Value)
              .Select(g => new Vaule
              {
                  FitValue = g.FirstOrDefault().Value,
                  FitCount = g.Count()
              }).OrderByDescending(x => x.FitCount).ToList().FirstOrDefault().FitValue;
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值