| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 | using Newtonsoft.Json.Linq;using PTMedicalInsurance.Entity;using PTMedicalInsurance.Helper;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    {        /// <summary>        /// 获取交易枚举对于的code信息        /// </summary>        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();        }        /// <summary>        /// 将字符串转为JObject        /// </summary>        /// <param name="text"></param>        /// <returns></returns>        public static string ParseReturnObject(this string text)        {            if (string.IsNullOrEmpty(text) || !text.StartsWith("{")) {                return JsonHelper.setIrisReturnValue(-1, text, null).ToString();            }            return JsonHelper.setIrisReturnValue(0, "", JObject.Parse(text)).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));            }        }    }}
 |