29

私は次のようなものをコーディングしました:

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

によって参照されるEnvDTEへの参照を取得するために使用したい。属性引数への参照を取得する方法を知っており、 を使用できますが、文字列 が得られます。CodeElementtypeofValuetypeof(MyCustomType)

を使用する場合Value、文字列を分解してから型を見つけようとする必要がありますが、名前が同じで名前空間が異なる 2 つの型が存在する場合、複雑になります。

これを行う簡単な方法はありますか?

4

2 に答える 2

5

これを行う簡単な方法はありますか?

いいえ、そうは思いません。少なくとも <= VS2013 では、それCodeAttributeArgument以上進んでいないようです。これは残念です。彼らは次のようにリリースCodeAttributeArgument2する必要がありValueましたCodeExpr:\ ..

>=VS2014 を使用すると、Roslyn にアクセスできるようになり、簡単になるはず です。VS 拡張機能内で roslyn にアクセスする方法が正確にはわかりません。しばらくお待ちください。

属性を取得するには、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

その間、バックトラッキング ソリューションを使用することができます。基本的な考え方は、クラスから逆方向に追跡を開始し、クラスのパスにあるさまざまな名前空間/使用を追跡することです。これは、型を解決する場合に、実際のコンパイラが行うことをほぼシミュレートしようとします。

 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

それは間違いなく、 current よりもはるかに簡単ですCodeModel

于 2014-12-31T23:03:39.657 に答える
-2

T4/EnvDTE を使用して、特定の属性で装飾されたすべてのメソッドを取得する方法を説明した方法を試すことができます。

于 2014-10-27T18:33:12.017 に答える