问题描述
我编写了如下代码:
[Attrib(typeof(MyCustomType))]
public class TargetType
{
// .....
}
我想使用 EnvDTE
来获取对 typeof
引用的 CodeElement
的引用.我知道如何获取对属性参数的引用,并且可以使用 Value
,但这给了我字符串 typeof(MyCustomType)
.
I want to use EnvDTE
to get a reference to the CodeElement
referenced by the typeof
. I know how to get a reference to the attribute argument, and I can use Value
, but that gives me the string typeof(MyCustomType)
.
如果我使用 Value
,我必须分解字符串,然后尝试查找类型,如果有两个类型名称相同但名称空间不同,这会变得很麻烦.
If I use Value
, I have to break down the string and then try to find the type, which gets hairy if there are two types with the same name but different namespaces.
有更简单的方法吗?
推荐答案
有更简单的方法吗?
Is there an easier way to do this?
不,我不这么认为,至少对于 <= VS2013,似乎 CodeAttributeArgument
没有进一步发展,这很遗憾.他们应该已经发布 CodeAttributeArgument2
具有 Value
作为 CodeExpr
:..
No, I don't think so, atleast for a <= VS2013, it seems that the CodeAttributeArgument
doesn't go any further, which is shame. They should've released CodeAttributeArgument2
that has Value
as CodeExpr
:..
如果您使用 >=VS2014,您可以访问 Roslyn,并且它应该 变得更容易 - 不知道如何在 VS 扩展中访问 roslyn,还得拭目以待.
If you use >=VS2014, you can get access to Roslyn, and it should become easier - don't know exactly how you can access roslyn inside VS extension, have to wait and see.
为了获取属性,可以使用VS helper:
In order to get attributes, you can use VS helper:
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% 测试过.基本思想是从类开始向后跟踪,并跟踪类路径中的不同命名空间/使用.这试图模拟一个真正的编译器会做什么,如果它要解析一个类型:
In a meanwhile, you could use backtracking solution, this is not nice, but it should work, haven't tested it exactly 100%. The basic idea is to start tracking backwards from the class, and keep track of different namespaces/usings that arein the path of a class. This tries to simulate pretty much what a real compiler would do, if it's going to resolve a type:
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
For VS2015, an example of roslyn integration can be found from here: https://github.com/tomasr/roslyn-colorizer/blob/master/RoslynColorizer/RoslynColorizer.cs
肯定会比使用当前的 CodeModel
容易得多.
It'll definitely be A lot easier than it is with current CodeModel
.
这篇关于从 DTE 访问属性信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!