标签:tree value enc list 信息 proxy width index ring
原文:基于.net standard 的动态编译实现在前文[基于.net core 微服务的另类实现]结尾处,提到了如何方便自动的生成微服务的客户端代理,使对于调用方透明,同时将枯燥的东西使用框架集成,以提高使用便捷性。在尝试了基于 Emit 中间语言后,最终决定使用生成代码片段然后动态编译的模式实现。
private static StringBuilder CreateApiProxyCode() { var path = GetBinPath(); var dir = new DirectoryInfo(path); //获取项目中微服务接口文件 var files = dir.GetFiles("XZL*.Api.dll"); var codeStringBuilder = new StringBuilder(1024); //添加必要的using codeStringBuilder .AppendLine("using System;") .AppendLine("using System.Collections.Generic;") .AppendLine("using System.Text;") .AppendLine("using XZL.Infrastructure.ApiService;") .AppendLine("using XZL.Infrastructure.Defines;") .AppendLine("using XZL.Model;") .AppendLine("namespace XZL.ApiClientProxy") .AppendLine("{"); //namespace begin //处理每个文件中的接口信息 foreach (var file in files) { CreateApiProxyCodeFromFile(codeStringBuilder, file); } codeStringBuilder.AppendLine("}"); //namespace end return codeStringBuilder; }
private static void CreateApiProxyCodeFromFile(StringBuilder fileCodeBuilder, FileInfo file) { try { Assembly apiAssembly = Assembly.Load(file.Name.Substring(0, file.Name.Length - 4)); var types = apiAssembly .GetTypes() .Where(c => c.IsInterface && c.IsPublic) .ToList(); var apiSvcType = typeof(IApiService); bool isNeed = false; foreach (Type type in types) { //找出期望的接口类型 if (!apiSvcType.IsAssignableFrom(type)) { continue; } //找出接口的所有方法 var methods = type.GetMethods(BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance); if (!methods.Any()) { continue; } //定义代理类名,以及实现接口和继承RemoteServiceProxy fileCodeBuilder.AppendLine($"public class {type.FullName.Replace(".", "_")}Proxy :" + $"RemoteServiceProxy, {type.FullName}") .AppendLine("{"); //class begin //处理每个方法 foreach (var mth in methods) { CreateApiProxyCodeFromMethod(fileCodeBuilder, type, mth); } fileCodeBuilder.AppendLine("}"); //class end isNeed = true; } if (isNeed) { var apiRefAsms = apiAssembly.GetReferencedAssemblies(); refAssemblyList.Add(apiAssembly.GetName()); refAssemblyList.AddRange(apiRefAsms); } } catch { } }
private static void CreateApiProxyCodeFromMethod( StringBuilder fileCodeBuilder, Type type, MethodInfo mth) { var isMthReturn = !mth.ReturnType.Equals(typeof(void)); fileCodeBuilder.Append("public "); //添加返回值 if (isMthReturn) { fileCodeBuilder.Append(GetFriendlyTypeName(mth.ReturnType)).Append(" "); } else { fileCodeBuilder.Append(" void "); } //方法参数开始 fileCodeBuilder.Append(mth.Name).Append("("); var mthParams = mth.GetParameters(); if (mthParams.Any()) { var mthparaList = new List<string>(); foreach (var p in mthParams) { mthparaList.Add(GetFriendlyTypeName(p.ParameterType) + " " + p.Name); } fileCodeBuilder.Append(string.Join(",", mthparaList)); } //方法参数结束 fileCodeBuilder.Append(")"); //方法体开始 fileCodeBuilder.AppendLine("{"); if (isMthReturn) { //返回值 fileCodeBuilder.Append("return Invoke<") .Append(GetFriendlyTypeName(mth.ReturnType)) .Append(">"); } else { fileCodeBuilder.Append(" InvokeWithoutReturn"); } //拼接接口名及方法名 fileCodeBuilder.Append($"(\"{type.FullName}\",\"{mth.Name}\""); //方法本身参数 if (mthParams.Any()) { fileCodeBuilder.Append(",").Append(string.Join(",", mthParams.Select(t => t.Name))); } fileCodeBuilder.Append(");"); //方法体结束 fileCodeBuilder.AppendLine("}"); }
private static string GetFriendlyTypeName(Type type) { if (!type.IsGenericType) { return type.FullName; } string friendlyName = type.Name; int iBacktick = friendlyName.IndexOf(‘`‘); if (iBacktick > 0) { friendlyName = friendlyName.Remove(iBacktick); } friendlyName += "<"; Type[] typeParameters = type.GetGenericArguments(); for (int i = 0; i < typeParameters.Length; ++i) { string typeParamName = GetFriendlyTypeName(typeParameters[i]); friendlyName += (i == 0 ? typeParamName : "," + typeParamName); } friendlyName += ">"; return friendlyName; }
//缓存程序集依赖 var references = new List<MetadataReference>(); var refAsmFiles = new List<string>(); //系统依赖 var sysRefLocation = typeof(Enumerable).GetTypeInfo().Assembly.Location; refAsmFiles.Add(sysRefLocation); //refAsmFiles原本缓存的程序集依赖 refAsmFiles.Add(typeof(object).GetTypeInfo().Assembly.Location); refAsmFiles.AddRange(refAssemblyList.Select(t => Assembly.Load(t).Location).Distinct().ToList()); //传统.NetFramework 需要添加mscorlib.dll var coreDir = Directory.GetParent(sysRefLocation); var mscorlibFile = coreDir.FullName + Path.DirectorySeparatorChar + "mscorlib.dll"; if (File.Exists(mscorlibFile)) { references.Add(MetadataReference.CreateFromFile(mscorlibFile)); } var apiAsms = refAsmFiles.Select(t => MetadataReference.CreateFromFile(t)).ToList(); references.AddRange(apiAsms); //当前程序集依赖 var thisAssembly = Assembly.GetEntryAssembly(); if (thisAssembly != null) { var referencedAssemblies = thisAssembly.GetReferencedAssemblies(); foreach (var referencedAssembly in referencedAssemblies) { var loadedAssembly = Assembly.Load(referencedAssembly); references.Add(MetadataReference.CreateFromFile(loadedAssembly.Location)); } }
//定义编译后文件名 var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Proxy"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var apiRemoteProxyDllFile = Path.Combine(path, apiRemoteAsmName + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".dll"); var tree = SyntaxFactory.ParseSyntaxTree(codeBuilder.ToString()); var compilation = CSharpCompilation.Create(apiRemoteAsmName) .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)) .AddReferences(references) .AddSyntaxTrees(tree); //执行编译 EmitResult compilationResult = compilation.Emit(apiRemoteProxyDllFile); if (compilationResult.Success) { // Load the assembly apiRemoteAsm = Assembly.LoadFrom(apiRemoteProxyDllFile); } else { foreach (Diagnostic codeIssue in compilationResult.Diagnostics) { string issue = $"ID: {codeIssue.Id}, Message: {codeIssue.GetMessage()}," + $" Location: { codeIssue.Location.GetLineSpan()}, " + $"Severity: { codeIssue.Severity}"; AppRuntimes.Instance.Loger.Error("自动编译代码出现异常," + issue); } }
static ConcurrentDictionary<string, Object> svcInstance = new ConcurrentDictionary<string, object>(); var typeName = "XZL.ApiClientProxy." + typeof(TService).FullName.Replace(".", "_") + "Proxy"; object obj = null; if (svcInstance.TryGetValue(typeName, out obj) && obj != null) { return (TService)obj; } try { obj = (TService)apiRemoteAsm.CreateInstance(typeName); svcInstance.TryAdd(typeName, obj); } catch { throw new ICVIPException($"未找到 {typeof(TService).FullName} 的有效代理"); } return (TService)obj;
标签:tree value enc list 信息 proxy width index ring
原文地址:https://www.cnblogs.com/lonelyxmas/p/10226916.html