/// <summary> /// 转换参数 /// </summary> /// <param name="paras"></param> /// <returns></returns> public Dictionary<string, string> JosnToDictionary(string paras) { Dictionary<string, string> dictionaryPara = new Dictionary<string, string>(); if (!string.IsNullOrEmpty(paras)) { JObject jobj = JObject.Parse(paras); JToken jsonResult = jobj as JToken; foreach (JProperty item in jsonResult) { dictionaryPara.Add(item.Name, item.Value.ToString()); } } return dictionaryPara; }
/// <summary> /// 反射执行方法 /// </summary> /// <param name="assemblyName"></param> /// <param name="instPath"></param> /// <param name="methodName"></param> /// <param name="paras"></param> /// <returns></returns> public object AssemblyClass(string assemblyName, string instPath, string methodName, string paras, ref string errorMsg) { try { //解析程序集名称 List<string> assemblyNameList = new List<string>(); if (!string.IsNullOrEmpty(assemblyName)) { assemblyNameList = assemblyName.Split(',').ToList(); } //解析参数 Dictionary<string, string> dictionaryPara = JosnToDictionary(paras); //dll文件路径 string baseDirectory = AppDomain.CurrentDomain.BaseDirectory; string loadFile = AppDomain.CurrentDomain.BaseDirectory + "/" + assemblyNameList[0] + ".dll"; Assembly assembly = Assembly.LoadFile(loadFile); Type type = assembly.GetType(instPath); object instance = assembly.CreateInstance(instPath); MethodInfo[] info = type.GetMethods(); Type[] params_type = null; Object[] params_obj = null; for (int i = 0; i < info.Length; i++) { var md = info[i]; if (md.Name == methodName) { //参数集合 ParameterInfo[] paramInfos = md.GetParameters(); params_obj = new Object[paramInfos.Length]; params_type = new Type[paramInfos.Length]; foreach (var item in paramInfos) { foreach (var itemp in dictionaryPara) { if (item.Name == itemp.Key) { var propertyType = Nullable.GetUnderlyingType(item.ParameterType) ?? item.ParameterType; params_type[item.Position] = propertyType; if (propertyType.Name == "DateTime") { if (!string.IsNullOrEmpty(itemp.Value)) { params_obj[item.Position] = DateTime.Parse(itemp.Value); } else { params_obj[item.Position] = null; } } else { params_obj[item.Position] = !string.IsNullOrEmpty(itemp.Value) ? itemp.Value : ""; } break; } } } break; } } object value = type.GetMethod(methodName, params_type).Invoke(instance, params_obj); return value; } catch (Exception ex) { errorMsg = ex.Message; return null; } }