【发布时间】:2014-04-17 21:24:11
【问题描述】:
这是我在 * 上的第一个问题,您好 :)
是否可以在 t4 模板中使用 Assembly.Load() 按程序集名称加载程序集? 我想用它来获取加载程序集中具有 ServiceContract 属性的所有类型。
var loadedAssembly = Assembly.Load(assemblyName);
var types = from type in loadedAssembly.GetTypes()
where Attribute.IsDefined(type, typeof(ServiceContractAttribute))select type;
在我的模板所在的项目中引用了所需的程序集。我想通了
<#@ assembly name="$(TargetDir)\DesiredAssemblyName.dll" #>
var loadedAssembly = Assembly.GetAssembly(typeof(SomeType));
有效,但它似乎不是一个好的解决方案。此外,我希望该模板在构建后以及在 .csproj 中添加以下行时进行转换
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\
TextTemplating\v10.0\Microsoft.TextTemplating.targets"/>
<PropertyGroup>
<TransformOnBuild>true</TransformOnBuild>
</PropertyGroup>
<ItemGroup>
<!--Add VS\...\PublicAssemblies to the list of places
to look for assemblies used by templates.-->
<T4ReferencePath Include="..\Onii.Vespa.AppServer\"/>
</ItemGroup>
使用 Assembly.GetAssembly 的解决方案也不起作用。 谢谢大家的建议。
【问题讨论】:
-
你见过这个吗? *.com/questions/3434713/…
-
哇哦!您的情况与我的情况完全相同,具体到 wcf 属性检查!你解决了吗?我正在尝试使用 ShadowCopy 手动加载程序集,以防止 T4 模板中使用的 dll 出现锁定问题。
-
@julealgon 不幸的是没有。每当我知道结果发生变化时,我都会手动进行。