Is there a way to get type members and all subsequent base types?

I have an ITypeSymbol ' object . If I call GetMembers , it gives me members of the current type, not the base. I know that I can dig it using the BaseType property and have some iterative code to get all the properties.

Is there an easier way to extract all members regardless of level in the inheritance hierarchy?

+4
source share
1 answer

If you are looking for all the participants, regardless of whether they are available or not:

There is no public API for this, and the internal approach of the Roslyn team more or less matches what you described.

internal GetBaseTypesAndThis(). :

var tree = CSharpSyntaxTree.ParseText(@"
public class A
{
    public void AMember()
    {
    }
}

public class B : A
{
    public void BMember()
    {
    }
}

public class C: B  //<- We will be analyzing this type.
{
    public void CMember()
    {
    }
    //Do you want this to hide B.BMember or not?
    new public void BMember()
    {
    }
}");

var Mscorlib = MetadataReference.CreateFromAssembly(typeof(object).Assembly);
var compilation = CSharpCompilation.Create("MyCompilation",
    syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
var model = compilation.GetSemanticModel(tree);

var classC = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().Last();
var typeC = (ITypeSymbol)model.GetDeclaredSymbol(classC);
//Get all members. Note that accessibility isn't considered.
var members = typeC.GetBaseTypesAndThis().SelectMany(n => n.GetMembers());

:

GetAccessibleMembersInThisAndBaseTypes().

, . Roslyn # 5. .

SemanticModel.LookupSymbols()

+5

All Articles