从CodeAttribute中的参数获取CodeClass? [英] Getting the CodeClass from argument inside CodeAttribute?

查看:302
本文介绍了从CodeAttribute中的参数获取CodeClass?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作有些T4代码生成,为了这个,我需要的是BarAttribute的构造函数中传递的类型CodeClass。

I am working on some T4 code generation, for this I need the CodeClass of the type that is passed inside the constructor of BarAttribute.

class Baz { }
class Bar : Attribute { public Bar (Type type) {    } }

[Bar(typeof(Baz))]
public class Foo
{
}

这是我到目前为止,我的T4模板,我只是给CodeAttribute内[栏(typeof运算(巴兹))的功能:

This is what I have so far inside my T4 Template, I just give the CodeAttribute '[Bar(typeof(Baz))]' to the function:

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(巴兹),我怎么能得到巴兹的CodeClass(可以是解决方案中的另一个组件内)不反复THRU所有项目,ProjectItems,CodeElements等?

The function now will just write: typeof(Baz), how can I get the CodeClass of Baz (which can be inside another assembly within the solution) without iterating thru all Projects, ProjectItems, CodeElements, etc?

推荐答案

根据的威廉回复,你被限制在设计时的信息,这将是通过未解析文本到的属性。如果你有兴趣在寻找中引用的CodeClass中的的typeof 的关键字,而不诉诸递归,您可以使用的 VisualStudioAutomationHelper 类= http://t4-editor.tangible-engineering.com/T4-Editor-Visual-T4-Editing.html相对=nofollow>有形的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:

var project = VisualStudioHelper.CurrentProject;

var allClasses = VisualStudioHelper.GetAllCodeElementsOfType(project.CodeModel.CodeElements, EnvDTE.vsCMElement.vsCMElementClass, false);

allClasses.Cast<EnvDTE.CodeClass>().Single(x => x.Name == searchedClassName);

这篇关于从CodeAttribute中的参数获取CodeClass?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆