using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Dynamic; using System.Reflection; using System.Text; namespace CoreEntity.ESEntity { public class JObjectAccessor : DynamicObject { JToken obj; public JObjectAccessor(JToken obj) { this.obj = obj; } public override bool TryGetMember(GetMemberBinder binder, out object result) { result = null; if (obj == null) return false; var val = obj[binder.Name]; if (val == null) return false; result = Populate(val); return true; } public object Populate(JToken token) { var jval = token as JValue; if (jval != null) { return jval.Value; } else if (token.Type == JTokenType.Array) { var objectAccessors = new List(); foreach (var item in token as JArray) { objectAccessors.Add(Populate(item)); } return objectAccessors; } else { return new JObjectAccessor(token); } } } public class HitsItem { /// /// /// public string _index { get; set; } /// /// /// public string _type { get; set; } /// /// /// public string _id { get; set; } /// /// /// public string _score { get; set; } /// /// /// public string _routing { get; set; } /// /// /// public dynamic _source { get; set; } /// /// /// public dynamic fields { get; set; } /// /// /// public T entity() { var a = this._source!=null? Mapper(this._source): Activator.CreateInstance(); a = MapperFields(this.fields,a); return a; } /// /// /// public T entityDynamic() { var a = this._source != null ? MapperDynamic(this._source) : Activator.CreateInstance(); a = MapperFieldsDynamic(this.fields, a); return a; } /// /// /// public List sort { get; set; } /// /// 反射实现两个类的对象之间相同属性的值的复制 /// 适用于初始化新实体 /// /// 返回的实体 /// 数据源实体 /// 数据源实体 /// 返回的新实体 public static T Mapper(dynamic s) { T d = Activator.CreateInstance(); //构造新实例 string fieldName = ""; try { //获得类型 var Typed = typeof(T); foreach (PropertyInfo dp in Typed.GetProperties())//获得类型的属性字段 { if (s[dp.Name] != null && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同 { Object result = null; fieldName = dp.Name; if (s[dp.Name].GetType() == typeof(JObject)) { JToken token = (JToken)s[dp.Name]; result = token.ToObject(dp.PropertyType, JsonSerializer.Create()); //JsonConvert.PopulateObject(d); } else { if (dp.PropertyType.ToString().IndexOf("Collections.Generic.IList`1") < 0) { if (dp.PropertyType == typeof(DateTime?)) { if (s[dp.Name].Value.GetType() == typeof(Double)) result = ConvertIntDatetime((double)s[dp.Name].Value); if (s[dp.Name].Value.GetType() == typeof(String)) { var datestr = s[dp.Name].Value; try { result = Convert.ToDateTime(datestr); } catch (Exception ex1) { result = null; } } } else try { result = Convert.ChangeType(s[dp.Name], dp.PropertyType); } catch (Exception ex2) { Console.WriteLine(" dp.Name:"+ dp.Name + " dp.PropertyType: " + dp.PropertyType + " s[dp.Name].Value.GetType(): " + s[dp.Name].Value.GetType()+ " s[dp.Name]:" + s[dp.Name]); result = null; } } } dp.SetValue(d, result, null);//获得s对象属性的值复制给d对象的属性 } } } catch (Exception ex) { throw new Exception(fieldName); } return d; } /// /// 反射实现两个类的对象之间相同属性的值的复制 /// 适用于初始化新实体 /// /// 返回的实体 /// 数据源实体 /// 数据源实体 /// 返回的新实体 public static T MapperFields(dynamic s,T d) { try { if (s == null) return d; //获得类型 var Typed = typeof(T); foreach (PropertyInfo dp in Typed.GetProperties())//获得类型的属性字段 { if (s[dp.Name] != null && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同 { Object result = null; if (s[dp.Name].GetType() == typeof(JArray)) { JArray token = (JArray)s[dp.Name]; result = token[0]; if (dp.PropertyType == typeof(DateTime?)) result = ConvertIntDatetime(Convert.ToDouble(result)); else result = Convert.ChangeType(result, dp.PropertyType); //JsonConvert.PopulateObject(d); } else { if (dp.PropertyType == typeof(DateTime?)) result = ConvertIntDatetime(Convert.ToDouble(s[dp.Name])); else result = Convert.ChangeType(s[dp.Name], dp.PropertyType); } dp.SetValue(d, result, null);//获得s对象属性的值复制给d对象的属性 } } } catch (Exception ex) { throw ex; } return d; } /// /// 反射实现两个类的对象之间相同属性的值的复制 /// 适用于初始化新实体 /// /// 返回的实体 /// 数据源实体 /// 数据源实体 /// 返回的新实体 public static Dictionary MapperDynamic(Dictionary s) { Dictionary d = new Dictionary(); // try { //获得类型 var Typed = typeof(T); foreach (PropertyInfo dp in Typed.GetProperties())//获得类型的属性字段 { if (s[dp.Name] != null && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同 { JToken result = null; if (s[dp.Name].GetType() == typeof(JObject)) { //JToken token = (JToken)s[dp.Name]; result = s[dp.Name]; } else { if (dp.PropertyType == typeof(DateTime?)) result = ConvertIntDatetime((double)s[dp.Name]); else result = s[dp.Name]; } d.Add(dp.Name, result);//获得s对象属性的值复制给d对象的属性 } } } catch (Exception ex) { throw ex; } return d; } /// /// 反射实现两个类的对象之间相同属性的值的复制 /// 适用于初始化新实体 /// /// 返回的实体 /// 数据源实体 /// 数据源实体 /// 返回的新实体 public static Dictionary MapperFieldsDynamic(Dictionary s, Dictionary d) { try { if (s == null) return d; //获得类型 var Typed = typeof(T); foreach (PropertyInfo dp in Typed.GetProperties())//获得类型的属性字段 { if (s[dp.Name] != null && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同 { JToken result = null; if (s[dp.Name].GetType() == typeof(JArray)) { JArray token = (JArray)s[dp.Name]; result = token[0]; if (dp.PropertyType == typeof(DateTime?)) result = ConvertIntDatetime(Convert.ToDouble(result)); //else // ; } else { if (dp.PropertyType == typeof(DateTime?)) result = ConvertIntDatetime(Convert.ToDouble(s[dp.Name])); else result = s[dp.Name]; } d.Add(dp.Name, result);//获得s对象属性的值复制给d对象的属性 } } } catch (Exception ex) { throw ex; } return d; } public static DateTime ConvertIntDatetime(double utc) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); startTime = startTime.AddMilliseconds(utc); //startTime = startTime.AddHours(8);//转化为北京时间(北京时间=UTC时间+8小时 ) return startTime; } internal object MapperFields(object fields, object a) { throw new NotImplementedException(); } } }