获取类型信息的另一个程序/项目申报类型 [英] Get type information for type declared in another assembly/project

查看:280
本文介绍了获取类型信息的另一个程序/项目申报类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,正如标题所说,我想从使用罗斯林另一个大会宣布类型的类型信息。起初我试图通过引用的程序集手动寻找这样做,但意识到我没有命名空间信息



我期待下面的工作:

  VAR工作区= Roslyn.Services.Workspace.LoadSolution(solFilePath); 

的IDocument文件= NULL;
的IProject幻灯= NULL;

编译编译= Compilation.Create(测试);
名单,LT; CommonSyntaxTree>树=新的List< CommonSyntaxTree>();

的foreach(在workSpace.CurrentSolution.Projects VAR PROJ)
{
的foreach(在proj.Documents VAR DOC)
{
如果(文件。 filePath.ToLower()== filePath.ToLower())
{
档= DOC;
幻灯=凸出;
}
,否则trees.Add(doc.GetSyntaxTree());
}
}

VAR targetTree = file.GetSyntaxTree();

trees.Add(targetTree);
VAR compilation2 =(编辑)compilation.AddSyntaxTrees(树);

VAR semanticModel = compilation2.GetSemanticModel(targetTree);

无功根= targetTree.GetRoot();

变种的className = root.DescendantNodes()OfType&所述; ClassDeclarationSyntax方式>()了ToList()的ElementAt(1);

VAR性能= className.DescendantNodes()OfType< PropertyDeclarationSyntax>()
。凡(P => p.Modifiers.Select(SYN => syn.ToString()。 ToLower将()==公用)计数()方式> 0).ToList();

VAR所属类别= semanticModel.GetTypeInfo(properties.First());

VAR财产= properties.First();

VAR符号= semanticModel.GetDeclaredSymbol(财产);

VAR symbol2 = semanticModel.GetSymbolInfo(财产);



无论是所属类别也不 typeSyntax 有我需要其中的信息,我在做什么错在这里。



这是我解析类:

 类Foo 
{
公共酒吧酒吧{搞定;组; }

公共美孚(){}

公众诠释调用getInt(酒吧酒吧,诠释RET = 5){
返回RET;
}
}



我试图让输入信息的酒吧这是另一个程序定义的类。



感谢您的帮助。


< DIV CLASS =h2_lin>解决方案

不要叫 GetTypeInfo的。相反,一旦你的 PropertyDeclarationSyntax ,电话 GetDeclaredSymbol 与声明的语法,你将获得 IPropertySymbol 该属性。从那里,你可以抓住 IPropertySymbol.Type ,这就是你要找的类型。


So as the title says I'm trying to get type information from types declared in another assembly using Roslyn. Initially I tried to do this by manually looking through referenced assemblies but realised I didn't have namespace information.

I was expecting the below to work:

  var workSpace = Roslyn.Services.Workspace.LoadSolution(solFilePath);

  IDocument file = null;
  IProject proje = null;

  Compilation compilation = Compilation.Create("Test");
  List<CommonSyntaxTree> trees = new List<CommonSyntaxTree>();

  foreach (var proj in workSpace.CurrentSolution.Projects)
  {
    foreach (var doc in proj.Documents)
    {
      if (doc.FilePath.ToLower() == filePath.ToLower())
      {
        file = doc;
        proje = proj;
      }
      else trees.Add(doc.GetSyntaxTree());
    }
  }

  var targetTree = file.GetSyntaxTree();

  trees.Add(targetTree);
  var compilation2 = (Compilation)compilation.AddSyntaxTrees(trees);

  var semanticModel = compilation2.GetSemanticModel(targetTree);

  var root = targetTree.GetRoot();

  var className = root.DescendantNodes().OfType<ClassDeclarationSyntax>().ToList().ElementAt(1);

  var properties = className.DescendantNodes().OfType<PropertyDeclarationSyntax>()
      .Where(p => p.Modifiers.Select(syn => syn.ToString().ToLower() == "public").Count() > 0).ToList();

  var typeInfo = semanticModel.GetTypeInfo(properties.First());

  var property = properties.First();

  var symbol = semanticModel.GetDeclaredSymbol(property);

  var symbol2 = semanticModel.GetSymbolInfo(property);

Neither typeInfo nor typeSyntax have the information that I need in them, what am I doing wrong here?

This is the class I'm parsing:

   class Foo
  {
    public Bar bar { get; set; }

    public Foo() { }

    public int GetInt(Bar bar, int ret = 5 ){
      return ret;
    }
  }

I'm trying to get Type information on the Bar class which is defined in another assembly.

Thanks for any help.

解决方案

Don't call GetTypeInfo. Instead, once you have the PropertyDeclarationSyntax, call GetDeclaredSymbol with that declaration syntax, and you will get the IPropertySymbol for that property. From there, you can grab IPropertySymbol.Type, which is the type you're looking for.

这篇关于获取类型信息的另一个程序/项目申报类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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