using Newtonsoft.Json.Linq; using PTMedicalInsurance.Entity; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace PTMedicalInsurance.Common { public static class AppExtension { /// /// 获取交易枚举对于的code信息 /// public static string GetCode(this TradeEnum tradeEnum) { Type type = tradeEnum.GetType(); FieldInfo fd = type.GetField(tradeEnum.ToString()); return getFieldCode(fd); } public static ModeEnum GetMode(this TradeEnum tradeEnum) { Type type = tradeEnum.GetType(); FieldInfo fd = type.GetField(tradeEnum.ToString()); TradeAttribute attribute = getAttribte(fd); if (attribute != null) return attribute.Mode; // 默认值 return ModeEnum.REST; } public static TradeEnum GetByCode(this TradeEnum tradeEnum,string code) { Type type = tradeEnum.GetType(); FieldInfo[] fds = type.GetFields(); foreach (FieldInfo fd in fds) { TradeAttribute attribute = getAttribte(fd); if (code.Equals(attribute?.Code)) { return (TradeEnum)fd.GetValue(tradeEnum); } } return TradeEnum.DEFAULT; } private static TradeAttribute getAttribte(FieldInfo fd) { TradeAttribute attribute = null; if (fd == null) { return attribute; } var attributes = fd.GetCustomAttributes(typeof(TradeAttribute), false); if (attributes != null && attributes.Length == 1) { attribute = attributes.Single() as TradeAttribute; return attribute; } return attribute; } public static string Text(this JToken jtoken) { if (jtoken == null) return string.Empty; return jtoken.ToString(); } public static int Int(this JToken jtoken) { if (jtoken == null) return 0; return int.Parse(jtoken.ToString()); } private static string getFieldCode(FieldInfo fd) { TradeAttribute attribute = getAttribte(fd); return attribute?.Code; } public static void Extend(this JObject jObject,string key,object value) { if (jObject != null && !jObject.ContainsKey(key)) { jObject.Add(key, new JValue(value)); } } } }