从 CodeAttribute 内的参数获取 CodeClass?
我正在研究一些 T4 代码生成,为此我需要在 BarAttribute 构造函数内传递的类型的 CodeClass。
class Baz { }
class Bar : Attribute { public Bar (Type type) { } }
[Bar(typeof(Baz))]
public class Foo
{
}
这就是我到目前为止在 T4 模板中所拥有的内容,我只是将 CodeAttribute '[Bar(typeof(Baz))]' 赋予该函数:
private CodeClass GetType(CodeElement codeElement)
{
CodeAttribute attribute = (CodeAttribute)codeElement;
if (attribute.Name == "Bar")
{
foreach (CodeElement child in attribute.Children)
{
EnvDTE80.CodeAttributeArgument attributeArg = (EnvDTE80.CodeAttributeArgument)child;
WriteLine(attributeArg.Value);
}
}
return null;
}
该函数现在将只写:typeof(Baz),我怎样才能获得 CodeClass Baz(可以位于解决方案中的另一个程序集中)而不迭代所有项目、ProjectItems、CodeElements 等?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 William 的回复,您仅限于设计时信息,该信息将是传递给属性。如果您有兴趣查找 typeof 关键字中引用的 CodeClass,而不采用递归,则可以使用 VisualStudioAutomationHelper 类-editor.tangible-engineering.com/T4-Editor-Visual-T4-Editing.html" rel="nofollow noreferrer">tangible 的 T4 编辑器 模板库。你像这样使用它:
As per William's reply, you are limited to the design-time information, which will be the unparsed text passed to the attribute. If you are interested in finding the CodeClass referenced in the typeof keyword without resorting to recursion, you can use the
VisualStudioAutomationHelper
class found in tangible's T4 Editor template gallery. You use it like this: