• 反射案例


     private void ResendFailedNotAmountData()
            {
                //要调用方法的对象,该对象实现了ISyncManager接口
                var syncObj = GetSyncManager();
                SelectDatabase(_failureDatabaseId);
                Type type = typeof(ISyncManager);
                string[] hashObj = _redisHelper.HashGetAll(_notAmountDataKey);
                if (hashObj == null || hashObj.Count() <= 0) return;
                string[] keyValue = null;
                try
                {
                    using (var cancelTokenSource = new CancellationTokenSource(_timeOut))
                    {
                        foreach (var item in hashObj)
                        {
                            keyValue = GetItemStringArray(item);
                            var methodAndParams = keyValue[0].Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
                            MethodInfo method = type.GetMethod(methodAndParams[0]);
                            NewtonsoftJsonSerializer newtonsoft = new NewtonsoftJsonSerializer();
                            var typClass = Assembly.LoadFrom("Hx.DataSync.Entity.dll").GetExportedTypes().Where(te => te.GetInterface(methodAndParams[1]) != null).FirstOrDefault();
                            if (typClass == null)
                            {
                                Logger.Error("数据解析出问题");
                                return;
                            }
                            Logger.Info("字符串对象为:" + keyValue[1]);
                           //用type类型解析json字符串
                            var obj = newtonsoft.Deserialize(keyValue[1], typClass);
                            try
                            {
                                Task.Run(() =>
                                {
                                   //调用对象syncObj的方法
                                    var o = method.Invoke(syncObj, new object[] { obj });
                                    if ((bool)o)
                                    {
                                        _redisHelper.HashDelete(_notAmountDataKey, keyValue[0]);
                                    }
                                }, cancelTokenSource.Token).Wait();
                            }
                            catch (Exception ex)
                            {
                                Logger.Error($"错误数据提交时失败:{ex}");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error($"非额度数据重新发送是发生异常:{ex}");
                }                                                                                                                        
  • 相关阅读:
    Oracle面试题目及解答
    java -jar Incompatible argument to function
    plsql 查询到别的用户下面的表
    redis数据类型[string 、list 、 set 、sorted set 、hash]
    redis-cli 常用命令
    js判断浏览器,包括Edge浏览器
    HTMl5的sessionStorage和localStorage
    JS实现密码加密
    sprintf.js
    js-crc32
  • 原文地址:https://www.cnblogs.com/rengke2002/p/7442444.html
Copyright © 2020-2023  润新知