lundi 15 avril 2019

When I reflect objects of an external assembly in the T4 template,System.IO.FileNotFoundException

For example, I want to generate the template in assembly Mall.T4 by reflecting the objects in assembly Mall.Data. But I met System. IO. FileNotFoundException: failed to load file or assembly 'Microsoft. EntityFrameworkCore. Relational, Version = 2.2.3.0, Culture = neutral, PublicKeyToken = adb9793829ddae60 "or one of its dependencies. The specified file could not be found.The file name: "Microsoft. EntityFrameworkCore. Relational, Version = 2.2.3.0, Culture = neutral, PublicKeyToken = adb9793829ddae60"problem

I tried joining Microsoft in the current assembly. EntityFrameworkCore. Relational. DLL, and references in the T4 template. But it didn't work.

<#@ template language="C#" debug="True" #>
<#@ output extension="txt" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="$(TargetDir)Microsoft.EntityFrameworkCore.dll" #>
<#@ assembly name="$(TargetDir)Microsoft.EntityFrameworkCore.Relational.dll" #>
<#@ assembly name="$(TargetDir)Mall.Component.dll" #>
<#@ assembly name="$(TargetDir)Mall.Data.dll" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Mall.Component.UnitOfWork" #>
<#@ import namespace="Mall.Data.AccountData" #>
<#@ include file="T4Toolbox.tt" #>
<#@ include file="ServiceTemplate.tt" #>
<#@ include file="IServiceTemplate.tt" #>
<#@ include file="RepositoryTemplate.tt" #>
<#@ include file="IRepositoryTemplate.tt" #>
<#
    string curPath = Path.GetDirectoryName(Host.TemplateFile);
    string destPath = Path.Combine(curPath, "CodeFiles");
    if(!Directory.Exists(destPath))
    {
        Directory.CreateDirectory(destPath);
    }
    try
    {
        var assemblyEntity = typeof(Mall).Assembly;
        var assemblyEntityTypesInfo = assemblyEntity.DefinedTypes.Where(m => m.BaseType == typeof(DataBase)).ToList();
        foreach (var typeInfo in assemblyEntityTypesInfo)
        {
            var moduleName=typeInfo.Namespace.Split('.').LastOrDefault();//文件名前缀
            var modelName=moduleName.Substring(0, moduleName.Length - 4) + "Context";//数据库仓储名
            var tableName=typeInfo.Name;//数据库表名
            //IRepository生成
            CSharpTemplate template = new IRepositoryTemplate(moduleName,modelName,tableName);
            string fileName = string.Format(@"{0}\{2}Repository\{1}.cs", destPath, "I"+tableName+"Repository", moduleName);
            template.Output.Encoding = Encoding.UTF8;
            template.RenderToFile(fileName);
            //Repository生成
            template = new RepositoryTemplate(moduleName,modelName,tableName);
            fileName = string.Format(@"{0}\{2}Repository\Impl\{1}.cs", destPath, tableName+"Repository", moduleName);
            template.Output.Encoding = Encoding.UTF8;
            template.RenderToFile(fileName);
            //IService生成
            template = new IServiceTemplate(moduleName, tableName);
            fileName = string.Format(@"{0}\{2}Service\{1}.cs", destPath, "I"+tableName+"Service", moduleName);
            template.Output.Encoding = Encoding.UTF8;
            template.RenderToFile(fileName);
            //Service生成
            template = new ServiceTemplate(moduleName, tableName);
            fileName = string.Format(@"{0}\{2}Service\Impl\{1}.cs", destPath, tableName+"Service", moduleName);
            template.Output.Encoding = Encoding.UTF8;
            template.RenderToFile(fileName);
        }
    }
    catch(ReflectionTypeLoadException ex)
    {
        foreach(var exception in ex.LoaderExceptions)
        { #>
            <#=exception#>
        <#}
    }
#>





Aucun commentaire:

Enregistrer un commentaire