使用 T4 get 程序集进行反射 [英] Reflection with T4 get assemblies
问题描述
我想获取特定程序集中的所有类,这是我的代码
I want to get all of class in the specific assembly this is my code
var assembly=Assembly.GetExecutingAssembly();
var assemblies = assembly.GetTypes().Where(t => String.Equals(t.Namespace, "RepoLib.Rts.Web.Plugins.Profiler.Models", StringComparison.Ordinal)).ToArray();
当 c# 代码一切正常,我得到了我的程序集,但是在 t4
文件中写入时,我没有任何错误,但我的程序集计数是.
when c# code all thing is ok and i get my assemblies but when write in t4
file i dont have any error but my assemblies count is.
推荐答案
在 T4 模板中,执行程序集不是您的,而是来自 T4 引擎的程序集.
In a T4 template the executing assembly is not yours but one from the T4 engine.
要从程序集中访问类型,您必须执行以下步骤:
To access types from your assemblies, you have to perform the following steps:
向模板添加对程序集的引用.把它放在它的顶部:
Add a reference to your assembly to the template. Put that at the top of it:
<#@ assembly name="$(SolutionDir)<Project>\bin\Debug\<Project>.dll" #>
导入程序集的命名空间.将其放在上一行下方的某个位置:
Import the namespace of your assembly. Put that somewhere below the previous line:
<#@ import namespace="<Project>.<Namespace>" #>
要访问该程序集中的类型,请选择其中之一并从中获取程序集:
To access the types in this assembly, pick one of them and get the assembly from it:
var assembly = typeof(<Type in assembly>).Assembly;
var types = assembly.GetTypes()
.Where(t => String.Equals(
t.Namespace,
"RepoLib.Rts.Web.Plugins.Profiler.Models",
StringComparison.Ordinal))
.ToArray();
这篇关于使用 T4 get 程序集进行反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!