예제 #1
0
        public string EmitSetGetObject(string name)
        {
            var model = new Model();

            CommHelper.EmitSet(model, "Name", name);

            return(CommHelper.EmitGet(model, "Name") as string);
        }
예제 #2
0
        public string EmitSetGetGenericType(string name)
        {
            var model = new Model();

            CommHelper.EmitSet(model, "Name", name);

            return(CommHelper.EmitGet <Model, string>(model, "Name"));
        }
예제 #3
0
        /// <summary>
        /// 获取consul上的配置信息
        /// </summary>
        /// <typeparam name="T">consul配置信息类型</typeparam>
        /// <param name="consulIP">consul服务IP</param>
        /// <param name="consulPort">consul服务端口</param>
        /// <param name="prefixKey">key前缀,一般用来区分不同服务</param>
        /// <returns>consul配置信息</returns>
        public static T GetConfig <T>(string consulIP, int consulPort, string prefixKey) where T : class, new()
        {
            Dictionary <string, string> dicPair;

            using (var kv = new ConsulKV(consulIP, consulPort, prefixKey))
            {
                //获取所有key/value
                dicPair = kv.List();
            }

            //创建对象
            var obj = new T();

            Type type = typeof(T);
            //获取对象属性
            var pros = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            //给对象属性赋值
            foreach (var pro in pros)
            {
                //特性
                var attr = pro.GetCustomAttributes <ConsulConfigAttribute>().FirstOrDefault();
                var key  = $"{prefixKey}{attr.Key}";
                if (attr == null || dicPair.ContainsKey(key) == false)
                {
                    continue;
                }

                //字符串值
                var strValue = dicPair[key];

                //如果是字符串类型
                if (pro.PropertyType == typeof(string))
                {
                    CommHelper.EmitSet(obj, pro.Name, strValue);
                }
                //如果是日期类型
                else if (pro.PropertyType == typeof(DateTime))
                {
                    var value = Convert.ToDateTime(strValue);
                    CommHelper.EmitSet(obj, pro.Name, value);
                }
                else
                {
                    var value = JsonConvert.DeserializeObject(strValue, pro.PropertyType);
                    CommHelper.EmitSet(obj, pro.Name, value);
                }
            }

            return(obj);
        }
예제 #4
0
        /// <summary>
        /// 设置更新者字段值
        /// </summary>
        /// <param name="model">表对象</param>
        /// <param name="oper">操作者信息</param>
        protected void SetUpdateFields <T>(T model, OperatorInfo oper) where T : class
        {
            //更新者
            if (CommHelper.IsExistProperty(model, FieldName_Updater))
            {
                CommHelper.EmitSet(model, FieldName_Updater, oper.LoginName);
            }

            //更新时间
            if (CommHelper.IsExistProperty(model, FieldName_UpdateTime))
            {
                CommHelper.EmitSet(model, FieldName_UpdateTime, DateTime.Now);
            }
        }
예제 #5
0
        public void Comparative(int times)
        {
            Model    model = new Model();
            TimeSpan ts1;
            DateTime time1 = DateTime.Now;

            for (int i = 0; i < times; i++)
            {
                CommHelper.EmitSet <Model>(model, "Name", "测试1");
            }
            DateTime time2 = DateTime.Now;

            ts1 = time2 - time1;
            Console.WriteLine("emit:" + ts1.TotalMilliseconds);

            DateTime t3 = DateTime.Now;

            for (int i = 0; i < times; i++)
            {
                CommHelper.ReflectSet(model, "Name", "测试2");
            }
            DateTime t4 = DateTime.Now;

            ts1 = t4 - t3;
            Console.WriteLine("reflect" + ts1.TotalMilliseconds);

            DateTime t5 = DateTime.Now;

            for (int i = 0; i < times; i++)
            {
                model.Name = "测试3";
            }
            DateTime t6 = DateTime.Now;

            ts1 = t6 - t5;
            Console.WriteLine("直接赋值" + ts1.TotalMilliseconds);
        }