/// <summary> /// 加载服务组件 /// </summary> public void LoadSrv() { if (string.IsNullOrEmpty(SrvConfig.SrvDir)) { SrvConfig.SrvDir = "SrvDLL"; } if (!Directory.Exists(SrvConfig.SrvDir)) { Directory.CreateDirectory(SrvConfig.SrvDir); } string[] srvDLLs = Directory.GetFiles(SrvConfig.SrvDir); foreach (string file in srvDLLs) { if (loadDLL.Contains(file)) { continue; } else { loadDLL.Add(file); Assembly assembly = Assembly.LoadFile(file); Type[] allTypes = assembly.GetTypes(); foreach (Type type in allTypes) { if (type.IsClass) { SrvAttribute srvAttribute = type.GetCustomAttribute <SrvAttribute>(); if (srvAttribute != null) { // SrvPath srvPath = new SrvPath(); srvPath.SrvName = srvAttribute.SrvName; srvPath.ClsName = type.FullName; srvPath.DLLPath = file; srvPath.SrvAssembly = assembly; srvPath.SrvType = type; srvPath.SrvMethod = new Dictionary <string, string>(); srvPath.SrvMethodInfo = new Dictionary <string, MethodInfo>(); dicSrv[srvPath.SrvName] = srvPath; MethodInfo[] methodInfos = type.GetMethods(); foreach (MethodInfo info in methodInfos) { SrvAttribute srvMethod = info.GetCustomAttribute <SrvAttribute>(); if (srvMethod != null) { srvPath.SrvMethod[srvMethod.SrvName] = info.Name; srvPath.SrvMethodInfo[srvMethod.SrvName] = info; } } } } } } } }
/// <summary> /// 加载dll /// </summary> public static void LoadDll() { if (!File.Exists("DynamicInterfaceCls.dll")) { return; } byte[] allStram = File.ReadAllBytes("DynamicInterfaceCls.dll"); Assembly assembly = Assembly.Load(allStram); Type[] allcls = assembly.GetTypes(); foreach (Type type in allcls) { SrvAttribute srvAttribute = type.GetCustomAttribute <SrvAttribute>(); if (srvAttribute != null) { dic_Objet[type.Name] = Activator.CreateInstance(type); } } }
/// <summary> /// 处理源码 /// </summary> /// <param name="type"></param> /// <param name="name"></param> /// <returns></returns> public static string GenerateCode(Type type, string name) { if (!type.IsInterface) { return(null); } StringBuilder sb = new StringBuilder(); sb.Append(" using System;"); sb.Append(Environment.NewLine); sb.Append(" using CommonClass; "); sb.Append(Environment.NewLine); sb.Append(" using System.Collections.Generic; "); sb.Append(Environment.NewLine); sb.Append("namespace DynamicCodeGenerate"); sb.Append(Environment.NewLine); sb.Append("{"); sb.Append(Environment.NewLine); sb.AppendFormat(" public class {0}:{1}", name, type.FullName); sb.Append(Environment.NewLine); sb.Append(" {"); sb.Append(Environment.NewLine); string srvName = name; SrvAttribute srvAttribute = type.GetCustomAttribute <SrvAttribute>(); if (srvAttribute != null) { srvName = srvAttribute.SrvName; } MethodInfo[] methodInfos = type.GetMethods(); StringBuilder paramBuilder = new StringBuilder(); foreach (MethodInfo method in methodInfos) { paramBuilder.Clear(); string returnType = method.ReturnType.FullName; if (returnType.Equals("System.Void")) { returnType = " void "; } sb.AppendFormat("public {0} {1} (", returnType, method.Name); ParameterInfo[] parameters = method.GetParameters(); foreach (ParameterInfo parameter in parameters) { sb.AppendFormat("{0} {1},", parameter.ParameterType, parameter.Name); paramBuilder.Append(" srvParam = new SrvParam(); "); paramBuilder.Append(Environment.NewLine); paramBuilder.AppendFormat(" srvParam.ParamName =\"{0}\";", parameter.Name); paramBuilder.Append(Environment.NewLine); paramBuilder.AppendFormat(" srvParam.ParamType =\"{0}\";", parameter.ParameterType); paramBuilder.Append(Environment.NewLine); paramBuilder.AppendFormat(" srvParam.ParamObj ={0};", parameter.Name); paramBuilder.Append(" srvRquest.SrvParam.Add(srvParam);"); } if (paramBuilder.Length > 0) { //有参数 sb.Remove(sb.Length - 1, 1); } sb.Append(") {"); //定义方法体 if (paramBuilder.Length > 0) { paramBuilder.Insert(0, " SrvParam "); } //处理方法体 string srvMethod = method.Name; srvAttribute = method.GetCustomAttribute <SrvAttribute>(); if (srvAttribute != null) { srvMethod = srvAttribute.SrvName; } sb.AppendLine(); sb.AppendLine("SrvRquest srvRquest = new SrvRquest();"); sb.AppendFormat("srvRquest.SrvName = \"{0}\";", srvName); sb.AppendLine(); sb.AppendFormat("srvRquest.SrvMethod =\"{0}\";", srvMethod); sb.AppendLine(); sb.AppendLine("srvRquest.SrvParam = new List<SrvParam>();"); sb.AppendLine(paramBuilder.ToString()); sb.Append("QuestResult result="); sb.AppendLine("IORemoteQuest.RemoteQuestNET(srvRquest);"); if (method.ReturnType.Name != "Void") { sb.AppendFormat(" return ({0})result.Result;", returnType); } sb.AppendLine(" }"); } sb.Append(Environment.NewLine); sb.Append(" }"); sb.Append(Environment.NewLine); sb.Append("}"); string code = sb.ToString(); Console.WriteLine(code); Console.WriteLine(); return(code); }