这是我在StackOverflow上的第一个问题,所以你好 :)
在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的解决方案也不起作用。感谢所有的建议。