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;
}