如何访问项目码元数据? [英] How to access project code meta data?

查看:175
本文介绍了如何访问项目码元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的VSPackage的,我需要其实际值替换引用属性在代码中。例如:

In my VSPackage I need to replace reference to a property in code with its actual value. For example

public static void Main(string[] args) {
    Console.WriteLine(Resource.HelloWorld);
}



我要的是取代Resource.HelloWorld以其实际值 - 也就是说,找到一流的资源,并得到其静态属性的HelloWorld的价值。是否Visual Studio中暴露任何API来处理项目的代码模型?它肯定有一个,因为这是非常相似的重命名变量的共同任务。我不想在输出程序集使用反射,因为它是缓慢的,它锁定了一会儿文件。

What I want is to replace "Resource.HelloWorld" with its actual value - that is, find class Resource and get value of its static property HelloWorld. Does Visual Studio expose any API to handle code model of the project? It definitely has one, because this is very similar to common task of renaming variables. I don't want to use reflection on output assembly, because it's slow and it locks the file for a while.

推荐答案

有为做到这一点,我知道没有直接的方式。可靠地得到一个AST出的Visual Studio(和改变它)的一直是一个大问题。的罗莎琳项目的目标之一是创造这样的一个统一的方式,因为很多工具窗口有自己做这样的东西的方式。

There is no straight forward way to do this that I know of. Reliably getting an AST out of Visual Studio (and changes to it) has always been a big problem. Part of the goal of the Rosalyn project is to create an unified way of doing this, because many tool windows had their own way of doing this sort of stuff.

有四种方式来做到这一点:

There are four ways to do this:


  1. 符号

  2. FileCodeModel +的CodeDOM

  3. 罗莎琳AST

  4. 秘境方法

  1. Symbols
  2. FileCodeModel + CodeDOM
  3. Rosalyn AST
  4. Unexplored Method

符号

我相信大多数工具窗口,如代码查看之类代码元素的搜索使用从编译版本创建的符号。这是不理想的,因为它是一个小更重量级的,很难保持同步。你不得不缓存符号,使这个不慢。使用反射,你可以看到的CodeView如何实现这一点。

I believe most tool windows such as the CodeView and things like Code Element Search use the symbols created from a compiled build. This is not ideal as it is a little more heavy weight and hard to keep in sync. You'd have to cache symbols to make this not slow. Using reflector, you can see how CodeView implements this.

此方法使用私有程序集。用于获取符号看起来像这样的代码:

This approach uses private assemblies. The code for getting the symbols would look something like this:

var compilerHost = new IDECompilerHost();

var typeEnumerator = (from compiler in compilerHost.Compilers.Cast<IDECompiler>()
                                          from type in compiler.GetCompilation().MainAssembly.Types
                                          select new Tuple<IDECompiler, CSharpType>(compiler, type));

foreach (var typeTuple in typeEnumerator)
{
    Trace.WriteLine(typeTuple.Item2.Name);
    var csType = typeTuple.Item2;
    foreach (var loc in csType.SourceLocations)
    {
       var file = loc.FileName.Value;
       var line = loc.Position.Line;
       var charPos = loc.Position.Character;
    }
}



FileCodeModel +的CodeDOM

您可以尝试使用EnvDTE服务来获得一个代码文档相关的FileCodeModel。这将让你的类和方法。但它不支持获取方法体。你跟马车COM搞乱。这种丑陋的,因为COM对象引用CodeFunction或CodeClass可以得到伤病而不让你知道,这意味着你必须保持自己的一面镜子。

You could try using the EnvDTE service to get the FileCodeModel associated with a Code Document. This will let you get classes and methods. But it does not support getting the method body. You're messing with buggy COM. This ugly because an COM object reference to a CodeFunction or CodeClass can get invalided without you knowing it, meaning you'd have to keep your own mirror.

< A HREF =http://blogs.msdn.com/b/visualstudio/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx相对=nofollow>罗莎琳AST

Rosalyn AST

这允许提供相同的功能,既FileCodeModel和符号。我一直在玩这一点,它实际上不是太糟糕了。

This allows provides the same capabilities as both FileCodeModel and Symbols. I've been playing with this and it's actually not too bad.

秘境法

您可以尝试获取与该代码文档关联的基础LanguageServiceProvider。但是,这真的很难拉断,并留下你的许多问题。

You could try getting the underlying LanguageServiceProvider that is associated with the Code Document. But this is really difficult to pull off, and leaves you with many issues.

这篇关于如何访问项目码元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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