反射案例
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}"); }