【问题标题】:.Net Core T4 Running transformation could not load file or assembly.Net Core T4 运行转换无法加载文件或程序集
【发布时间】:2020-12-25 10:15:36
【问题描述】:

我有一个 .Net Core Asp.Net MVC 应用程序,我正在尝试创建一个 T4 文件,该文件使用反射来列出控制器的所有方法。但是,当引用 Controller 类时,它会引发错误。我尝试添加 Mvc.Core NuGet 包,但最新版本是 2.2.5,并且由于某种原因它需要 v3.1.0.0。

Error       Running transformation: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.AspNetCore.Mvc.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
   at Microsoft.VisualStudio.TextTemplating5EB735DC155C701BA88F9010B0EBAB3638F86E339180B5A3766CEE0DDDB7B870EC569C6473D06A892E38A012E5B34C56690737513ADB101F2235F88AEABE2E1D.GeneratedTextTransformation.TransformText()
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at CallSite.Target(Closure , CallSite , Object )
   at Microsoft.VisualStudio.TextTemplating.TransformationRunner.PerformTransformation()

这是我的 tt 文件:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ template language="C#v3.1" #>
<#@ assembly name="System.Core" #>
<#@ output extension=".cs" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Reflection" #>
class Foo {
    <# Assembly asm = Assembly.GetAssembly(typeof(UserController)); #>
}

【问题讨论】:

  • 我从来没有想出最好的方法来做到这一点,但我最终发现 NSwag 是一个很好的解决方案,可以在我的控制器中创建所有方法的列表。

标签: c# .net asp.net-mvc t4


【解决方案1】:

您不能直接在 T4 模板中引用程序集类。 如果您想使用解决方案的程序集访问路径,您可以使用 Host 类 File.ReadAllText(this.Host.ResolvePath("Him.txt"));

【讨论】:

    【解决方案2】:
    <#@ assembly name="$(SolutionDir)YourProject\bin\Debug\Project.Name.dll" #>
    

    您可以访问在导入程序集中定义的类,棘手的是向您的 T4 模板公开内部类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 2020-04-23
      • 1970-01-01
      相关资源
      最近更新 更多