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.
Get type information for type declared in another assembly/project
Question
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.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow