zoukankan      html  css  js  c++  java
  • 引用.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()
    
    警告: 程序集绑定日志记录被关闭。
    要启用程序集绑定失败日志记录,请将注册表值 [HKLMSoftwareMicrosoftFusion!EnableLog] (DWORD)设置为 1。
    注意: 会有一些与程序集绑定失败日志记录关联的性能损失。
    要关闭此功能,请移除注册表值 [HKLMSoftwareMicrosoftFusion!EnableLog]。    Ark.Y2020.DBModel    D:myCode其他项目Ark2020Ark.Y2020.DBModelBCBenefitConsume.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相关的配置才可以

    方法一:

    参考: https://stackoverflow.com/questions/51550265/t4-template-could-not-load-file-or-assembly-system-runtime-version-4-2-0-0/54314778#54314778

    C:Users<user>AppDataLocalMicrosoftVisualStudio15.0_29f8d23adevenv.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文件里进行排除

  • 相关阅读:
    规约先行-(六)并发处理
    MySQL选择合适的方式存储时间
    规约先行-(五)集合处理
    规约先行-(四)OOP 规约
    12.20-LaTex git workflow
    6.25-ROS 软件度量
    6.19-rosdoc_lite and 文档构建工具
    12.27-ros-decision making
    12.3-分级并发有限状态机-SMACH
    12.07-rostest学习
  • 原文地址:https://www.cnblogs.com/ark/p/12572073.html
Copyright © 2011-2022 走看看