从DTE访问属性信息

时间:2012-03-01 17:15:50

标签: c# visual-studio attributes envdte vs-extensibility

我编写了如下内容:

[Attrib(typeof(MyCustomType))]
public class TargetType
{
  // .....
}

我想使用EnvDTE来获取CodeElement引用的typeof的引用。我知道如何获取属性参数的引用,我可以使用Value,但这会给我字符串typeof(MyCustomType)

如果我使用Value,我必须分解字符串然后尝试找到类型,如果有两个具有相同名称但名称空间不同的类型,它会变得毛茸茸。

有更简单的方法吗?

2 个答案:

答案 0 :(得分:4)

  

有更简单的方法吗?

不,我不这么认为,至少对于< = VS2013,似乎CodeAttributeArgument没有进一步,这是耻辱。他们应该发布CodeAttributeArgument2 Value作为CodeExpr:\ ..

如果你使用> = VS2014,你可以访问Roslyn,应该 变得更容易 - 不知道如何访问roslyn在VS扩展内部,必须拭目以待。

为了获取属性,您可以使用VS帮助程序:

public List<CodeElement> GetAllCodeElementsOfType(
    CodeElements elements, 
    vsCMElement elementType, 

    bool includeExternalTypes)
{
    var ret = new List<CodeElement>();

    foreach (CodeElement elem in elements)
    {
        // iterate all namespaces (even if they are external)
        // > they might contain project code
        if (elem.Kind == vsCMElement.vsCMElementNamespace)
        {
            ret.AddRange(
                GetAllCodeElementsOfType(
                    ((CodeNamespace)elem).Members, 
                    elementType, 
                    includeExternalTypes));
        }

        // if its not a namespace but external
        // > ignore it
        else if (elem.InfoLocation == vsCMInfoLocation.vsCMInfoLocationExternal && !includeExternalTypes)
            continue;

        // if its from the project
        // > check its members
        else if (elem.IsCodeType)
        {
            ret.AddRange(
                GetAllCodeElementsOfType(
                    ((CodeType)elem).Members, 
                    elementType, 
                    includeExternalTypes));
        }

        if (elem.Kind == elementType)
            ret.Add(elem);
    }
    return ret;
}

原始来源:https://github.com/PombeirP/T4Factories/blob/master/T4Factories.Testbed/CodeTemplates/VisualStudioAutomationHelper.ttinclude

与此同时,你可以使用回溯解决方案,这不是很好,但它应该工作,还没有完全100%测试它。基本思想是从类开始向后跟踪,并跟踪类中路径的不同名称空间/使用。这试图模拟真正的编译器会做什么,如果它要解决一个类型:

 var solution = (Solution2) _applicationObject.Solution;
var projects = solution.Projects;
var activeProject = projects
    .OfType<Project>()
    .First();

// locate my class.
var myClass = GetAllCodeElementsOfType(
    activeProject.CodeModel.CodeElements,
    vsCMElement.vsCMElementClass, false)
    .OfType<CodeClass2>()
    .First(x => x.Name == "Program");

// locate my attribute on class.
var mySpecialAttrib = myClass
    .Attributes
    .OfType<CodeAttribute2>()
    .First();



var attributeArgument = mySpecialAttrib.Arguments
    .OfType<CodeAttributeArgument>()
    .First();

string myType = Regex.Replace(
    attributeArgument.Value, // typeof(MyType)
    "^typeof.*\\((.*)\\)$", "$1"); // MyType*/

var codeNamespace = myClass.Namespace;
var classNamespaces = new List<string>();

while (codeNamespace != null)
{
    var codeNs = codeNamespace;
    var namespaceName = codeNs.FullName;

    var foundNamespaces = new List<string> {namespaceName};

    // generate namespaces from usings.
    var @usings = codeNs.Children
        .OfType<CodeImport>()
        .Select(x =>
            new[]
            {
                x.Namespace,
                namespaceName + "." + x.Namespace
            })
        .SelectMany(x => x)
        .ToList();

    foundNamespaces.AddRange(@usings);

    // prepend all namespaces:
    var extra = (
        from ns2 in classNamespaces
        from ns1 in @usings
        select ns1 + "." + ns2)
        .ToList();

    classNamespaces.AddRange(foundNamespaces);
    classNamespaces.AddRange(extra);

    codeNamespace = codeNs.Parent as CodeNamespace;
    if (codeNamespace == null)
    {
        var codeModel = codeNs.Parent as FileCodeModel2;
        if (codeModel == null) return;

        var elems = codeModel.CodeElements;
        if (elems == null) continue;

        var @extraUsings = elems
            .OfType<CodeImport>()
            .Select(x => x.Namespace);

        classNamespaces.AddRange(@extraUsings);
    }
}

// resolve to a type!
var typeLocator = new EnvDTETypeLocator();
var resolvedType = classNamespaces.Select(type =>
        typeLocator.FindTypeExactMatch(activeProject, type + "." + myType))
    .FirstOrDefault(type => type != null);

您也需要EnvDTETypeLocator

对于VS2015 ,可以在此处找到roslyn集成的示例:https://github.com/tomasr/roslyn-colorizer/blob/master/RoslynColorizer/RoslynColorizer.cs

它肯定比当前CodeModel容易得多。

答案 1 :(得分:-2)