我有一个 T4 模板,想在设计时运行它,但是一直出现以下错误。
Running transformation: System.IO.FileNotFoundException: Could not load
file or assembly 'System.Runtime, Version=4.2.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system
cannot find the file specified.
File name: 'System.Runtime, Version=4.2.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'
我的Visual Studios解决方案中包含了10个项目,所有这些项目都是针对.Net Core 2.0框架的。我的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();
#>
}
目前我只需要模板中已引用的程序集,它是一个 .Net Core 2.0 类库项目。我尝试在这个特定的库中添加 System.Runtime.dll 引用,但似乎没有任何区别。
我读了几个类似的问题,通常 .Net Core 似乎与 T4 模板存在问题,但大多数人的解决方案是针对 .Net Standard 库进行定位。我不确定这是否适用于我,因为我的整个解决方案都只涉及 .Net Core 项目。
编辑
我将所有项目更改为定位到 .Net Standard 2.0 而不是 .Net Core,这解决了我的初始问题,但现在我看到这个错误:
System.Reflection.ReflectionTypeLoadException: Unable to load one or
more of the requested types. Retrieve the LoaderExceptions property for
more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()