0

Say I have a base class B and a derived EB class. How can I check which base constructor each EB's constructor calls?

Is it possible to do that via semantic model?

The code that illustrates my attempt is as follows:

var typeSymbol = semanticModel.GetDeclaredSymbol(classDeclarationSyntax);
IMethodSymbol ctor1 = typeSymbol.Constructors[0];
IMethodSymbol ctor2 = typeSymbol.Constructors[1];

I can't track any public interface (like IConstructorSymbol) so I could cast ctor0 and ctor1 to.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
user3284063
  • 665
  • 5
  • 20
  • That's not part of metadata; you need the source. – SLaks Oct 24 '17 at 16:26
  • Allright, I've somehow circumvented the limitaion by reverting back to syntax, then again to semantics, then again to syntax, etc. until top constructor is reached. My question now is why semantics does not store this kind of information (chain of contructors). Isn't it what semantic model is about? – user3284063 Oct 24 '17 at 19:20

1 Answers1

0

The semantic model is about metadata (classes and their members), not about the control flow / implementation within actual code.

This is why it's possible to get a semantic model for referenced assemblies (even though Roslyn is not a decompiler).

IOW, the semantic model corresponds to what you see in the Object Browser.

You need to use the syntax tree to see the contents of the constructor.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964