您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎么在.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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。