从 CodeAttribute 内的参数获取 CodeClass?

发布于 2024-11-16 12:50:37 字数 808 浏览 2 评论 0 原文

我正在研究一些 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 等?

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
{
}

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;
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流星番茄 2024-11-23 12:50:37

根据 William 的回复,您仅限于设计时信息,该信息将是传递给属性。如果您有兴趣查找 typeof 关键字中引用的 CodeClass,而不采用递归,则可以使用 VisualStudioAutomationHelper 类-editor.tangible-engineering.com/T4-Editor-Visual-T4-Editing.html" rel="nofollow noreferrer">tangible 的 T4 编辑器 模板库。你像这样使用它:

var project = VisualStudioHelper.CurrentProject;

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文