基于.net standard 的动态编译实现代码(2)

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;

总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdsxdp.html