【发布时间】: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