123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- 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<object>();
- foreach (var item in token as JArray)
- {
- objectAccessors.Add(Populate(item));
- }
- return objectAccessors;
- }
- else
- {
- return new JObjectAccessor(token);
- }
- }
- }
- public class HitsItem<T>
- {
- /// <summary>
- ///
- /// </summary>
- public string _index { get; set; }
- /// <summary>
- ///
- /// </summary>
- public string _type { get; set; }
- /// <summary>
- ///
- /// </summary>
- public string _id { get; set; }
- /// <summary>
- ///
- /// </summary>
- public string _score { get; set; }
- /// <summary>
- ///
- /// </summary>
- public string _routing { get; set; }
- /// <summary>
- ///
- /// </summary>
- public dynamic _source { get; set; }
- /// <summary>
- ///
- /// </summary>
- public dynamic fields { get; set; }
- /// <summary>
- ///
- /// </summary>
- public T entity()
- {
- var a = this._source!=null? Mapper<T>(this._source): Activator.CreateInstance<T>();
- a = MapperFields<T>(this.fields,a);
- return a;
- }
- /// <summary>
- ///
- /// </summary>
- public T entityDynamic()
- {
- var a = this._source != null ? MapperDynamic<T>(this._source) : Activator.CreateInstance<T>();
- a = MapperFieldsDynamic<T>(this.fields, a);
- return a;
- }
- /// <summary>
- ///
- /// </summary>
- public List<string> sort { get; set; }
-
- /// <summary>
- /// 反射实现两个类的对象之间相同属性的值的复制
- /// 适用于初始化新实体
- /// </summary>
- /// <typeparam name="D">返回的实体</typeparam>
- /// <typeparam name="S">数据源实体</typeparam>
- /// <param name="s">数据源实体</param>
- /// <returns>返回的新实体</returns>
- public static T Mapper<T>(dynamic s)
- {
- T d = Activator.CreateInstance<T>(); //构造新实例
- 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;
- }
- /// <summary>
- /// 反射实现两个类的对象之间相同属性的值的复制
- /// 适用于初始化新实体
- /// </summary>
- /// <typeparam name="D">返回的实体</typeparam>
- /// <typeparam name="S">数据源实体</typeparam>
- /// <param name="s">数据源实体</param>
- /// <returns>返回的新实体</returns>
- public static T MapperFields<T>(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;
- }
- /// <summary>
- /// 反射实现两个类的对象之间相同属性的值的复制
- /// 适用于初始化新实体
- /// </summary>
- /// <typeparam name="D">返回的实体</typeparam>
- /// <typeparam name="S">数据源实体</typeparam>
- /// <param name="s">数据源实体</param>
- /// <returns>返回的新实体</returns>
- public static Dictionary<string, JToken> MapperDynamic<T>(Dictionary<string, JToken> s)
- {
- Dictionary<string, JToken> d = new Dictionary<string, JToken>(); //
- 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;
- }
- /// <summary>
- /// 反射实现两个类的对象之间相同属性的值的复制
- /// 适用于初始化新实体
- /// </summary>
- /// <typeparam name="D">返回的实体</typeparam>
- /// <typeparam name="S">数据源实体</typeparam>
- /// <param name="s">数据源实体</param>
- /// <returns>返回的新实体</returns>
- public static Dictionary<string, JToken> MapperFieldsDynamic<T>(Dictionary<string, JToken> s, Dictionary<string, JToken> 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<T1>(object fields, object a)
- {
- throw new NotImplementedException();
- }
- }
- }
|