Abedron
Abedron

Reputation: 781

How get BaseType from IdentifierNameSyntax

I try make analyser which will detect BaseType IB from class A inside list.Add(typeof(A));. I have A as IdentifierNameSyntax but there is not method for getting base IB. Or exist? Can you help me?

class Consolidator
{
...
    public void Warner()
    {
        list.Add(typeof(A));
    }
...
}

class A : IB
{
...
}

Upvotes: 1

Views: 194

Answers (2)

Jason Malinowski
Jason Malinowski

Reputation: 19021

In a Roslyn analyzer, you need to use the SemanticModel to bind the thing inside the typeof. Call SemanticModel.GetSymbolInfo() and that'll give you an INamedTypeSymbol. From there you can inspect the base types.

Upvotes: 1

Vulpex
Vulpex

Reputation: 1083

I think what you're looking for is the Type.BaseType Property. For more details check out the MSDN documentation: Type.BaseType

using System;
                    
public class Program
{
    public static void Main()
    {
        var variable = new A();
        Console.WriteLine(variable.GetType().BaseType);
    }
}
class IB
{
//
}

class A : IB
{
//
}

Upvotes: 0

Related Questions