T4模板中反射Domain: GetTypes() 失败
yiyungent opened this issue · 0 comments
yiyungent commented
// 当前 Template.tt 所在的模板路径<br> // Host.Template 是当前文本模板 Template.tt 的路径
var currentPath = Path.GetDirectoryName(Host.TemplateFile);
// 解决方案路径: F:\Com\me\Repos\remember\src\Libraries
string solutionPath = currentPath.Replace("\\Repositories\\Auto\\Interface", "");
// 实体文件所在路径
string modelFile = Path.Combine(solutionPath, @"Domain\bin\Debug\netcoreapp3.1\Domain.dll");
// 读取
byte[] fileData = File.ReadAllBytes(modelFile);
Assembly assembly = Assembly.Load(fileData);
IEnumerable<Type> entityTypes = assembly.GetTypes().Where(m=>!m.IsAbstract && m.IsClass && m.BaseType.FullName != "System.Object");
foreach(Type type in entityTypes)
{
string entityName = type.Name;
string fileName = "I" + entityName + "Repository.cs";
Severity Code Description Project File Line Suppression State
Error Running transformation: 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()
at Microsoft.VisualStudio.TextTemplating9BD83D43DB5372F3AE00925B301F1CC69A4AB6B2706C61321E51DD28C9414F2EE648FE62DECE57E77FE639D348D8028DA62F7A08C0BFAD77C41FC95A412FAE8E.GeneratedTextTransformation.TransformText() in F:\Com\me\Repos\Remember.Core\src\Libraries\Repositories\Auto\Interface\T4_InterfaceRepository.tt:line 33 Repositories F:\Com\me\Repos\Remember.Core\src\Libraries\Repositories\Auto\Interface\T4_InterfaceRepository.tt 33