引用.net Core类时T4模板无法加载文件或程序集“ System.Runtime,版本= 4.2.2.0”
用.net Core 编写的T4模板类, 在T4里引用运行时,会有
错误 正在运行转换: System.IO.FileNotFoundException: 未能加载文件或程序集“System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。 文件名:“System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” 在 Microsoft.VisualStudio.TextTemplatingDF348CB3FB09E8E166E437124F9F88342823FF1D21BC7B73048E47A611D6DC38AD43D38B26E23A35527758646C26C0D989C154CDCD9B21719CC1A062236A2570.GeneratedTextTransformation.TransformText() 在 Microsoft.VisualStudio.TextTemplating.TransformationRunner.PerformTransformation() 警告: 程序集绑定日志记录被关闭。 要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD)设置为 1。 注意: 会有一些与程序集绑定失败日志记录关联的性能损失。 要关闭此功能,请移除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。 Ark.Y2020.DBModel D:\myCode\其他项目\Ark2020\Ark.Y2020.DBModel\BCBenefitConsume.tt 1
在使用T4的模板,代码类似
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Reflection" #> <#@ import namespace="Services.Resources.DataTransferObjects.Infrastructures" #> <#@ import namespace="System.Collections.Generic" #> <#@ assembly name="$(TargetDir)Services.dll" #> <#@ output extension=".cs" #> public class AdminDTO { <#var editableObjs = Assembly .GetAssembly(typeof(GenericEditable<>)) .GetTypes() .Where(p => p.BaseType != null && p.BaseType.IsGenericType && p.BaseType.GetGenericTypeDefinition() == (typeof(GenericEditable<>))) .ToList(); #> }
即使项目引用System.Runtime.dll也是没有用的, 这个问题的本质是Vs2019工具在运行T4程序时的问题, 所以可以修改Vs相关的配置才可以
方法一:
C:\Users\<user>\AppData\Local\Microsoft\VisualStudio\15.0_29f8d23a\devenv.exe.config
里有<configuration>
-> <runtime>
-> <assemblyBinding>
<dependentAssembly> <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="4.0.0.0"/> </dependentAssembly>
方法二:
上面说了,问题本质是Vs的问题, 所以我们不用.net core编写T4的辅助类即可,使用.net Framework的项目,然后把相关的Dll复制到解决方案目录下
T4的模板如下:
<#@ template debug="Flase" hostspecific="True" language="C#" #> <#@ assembly name="$(SolutionDir)T4dll/TC.Ab.T4.dll" #> <#@ assembly name="$(SolutionDir)T4dll/MySql.Data.dll" #> <#@ import namespace="TC.Ab.T4" #> <#@ import namespace="System.Text.RegularExpressions" #> <#@ import namespace="System.Diagnostics" #> <#@ output extension=".cs" #> <# //Debugger.Launch(); Debugger.Break();//调试用template debug="True" hostspecific="True" language="C#" DbField dbRender = new DbField(this.Host.TemplateFile,"TCItravelOrder");//数据库链接名称可以不传,默认MetaDataDB dbRender.NamespaceStr="TC.itravel.Admin.DBModel"; dbRender.OnlyTable.Add("BCBenefitConsume");//只要生成的表,区分大小写 this.WriteLine(dbRender.Render()); #>
这种情况只是Vs使用了Framework版本的类, 项目本身还是core, 所以不影响项目的发布,如果是Docker发布,可以在.dockerignore文件里进行排除
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库