C# 当我在T4模板中反映外部程序集的对象时,;System.IO.FileNotFoundException

C# 当我在T4模板中反映外部程序集的对象时,;System.IO.FileNotFoundException,c#,reflection,t4,C#,Reflection,T4,例如,我想通过在assembly Mall.Data中反映对象来生成assembly Mall.T4中的模板。但我遇到了系统。木卫一。FileNotFoundException:无法加载文件或程序集“Microsoft”。EntityFrameworkCore。Relational,Version=2.2.3.0,Culture=neutral,PublicKeyToken=adb9793829ddae60”或其依赖项之一。找不到指定的文件。文件名为:“Microsoft”。EntityFram

例如,我想通过在assembly Mall.Data中反映对象来生成assembly Mall.T4中的模板。但我遇到了系统。木卫一。FileNotFoundException:无法加载文件或程序集“Microsoft”。EntityFrameworkCore。Relational,Version=2.2.3.0,Culture=neutral,PublicKeyToken=adb9793829ddae60”或其依赖项之一。找不到指定的文件。文件名为:“Microsoft”。EntityFrameworkCore。关系型,版本=2.2.3.0,区域性=中性,PublicKeyToken=adb9793829ddae60“问题

我尝试在当前的assembly.EntityFrameworkCore.Relational.DLL中加入Microsoft,并在T4模板中加入引用,但没有成功

<#@ 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#>
        <#}
    }
#>

m、 BaseType==typeof(数据库)).ToList();
foreach(assemblyEntityTypesInfo中的变量typeInfo)
{
var moduleName=typeInfo.Namespace.Split('.').LastOrDefault()//文件名前缀
var modelName=moduleName.Substring(0,moduleName.Length-4)+“Context”//数据库仓储名
var tableName=typeInfo.Name//数据库表名
//间接的生成
CSharpTemplate template=新的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(文件名);
//存储库生成
模板=新的RepositoryTemplate(moduleName、modelName、tableName);
fileName=string.Format(@“{0}\{2}Repository\Impl\{1}.cs”,destPath,tableName+“Repository”,moduleName);
template.Output.Encoding=Encoding.UTF8;
template.RenderToFile(文件名);
//伊瑟薇生成
模板=新的IServiceTemplate(moduleName,tableName);
fileName=string.Format(@“{0}\{2}Service\{1}.cs”,destPath,“I”+tableName+“Service”,moduleName);
template.Output.Encoding=Encoding.UTF8;
template.RenderToFile(文件名);
//服务生成
模板=新服务模板(moduleName,tableName);
fileName=string.Format(@“{0}\{2}Service\Impl\{1}.cs”,destPath,tableName+“Service”,moduleName);
template.Output.Encoding=Encoding.UTF8;
template.RenderToFile(文件名);
}
}
捕获(ReflectionTypeLoadException ex)
{
foreach(ex.LoaderExceptions中的var异常)
{ #>

是否可以在生成项目/解决方案后通过转换模板来解决此问题?

是否可以在生成项目/解决方案后通过转换模板来解决此问题