您好,登录后才能下订单哦!
今天就跟大家聊聊有关怎么在.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); } }
结语
在经过以上处理后,虽算不上完美,但顺利的实现了我们期望的样子,在之前的GetService中,当发现属于远程服务的时候,只需要类似如下形式返回代理对象即可。同时为增加调用更加顺畅,我们将此编译的时机定在了发生在程序启动的时候,ps 当然或许还有一些其他更合适的时机.
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;
看完上述内容,你们对怎么在.net中利用standard 实现一个动态编译功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。