Get Base Class for a Type in Class Hierarchy

Get base class for a type in class hierarchy

I think std::is_base_of can help you

#include <type_traits>

std::is_base_of<B, D>()

If D is derived from B or if both are the same non-union class,
provides the member constant value equal to true. Otherwise value is
false.

You can use it to check if a class is base class of another or not :

std::is_base_of<A, A>()   // Base<A>::Type == A

std::is_base_of<A, B>() // Base<B>::Type == A

std::is_base_of<A, C>() // Base<C>::Type == A

List all base classes in a hierarchy of given class?

inspect.getmro(cls) works for both new and old style classes and returns the same as NewClass.mro(): a list of the class and all its ancestor classes, in the order used for method resolution.

>>> class A(object):
>>> pass
>>>
>>> class B(A):
>>> pass
>>>
>>> import inspect
>>> inspect.getmro(B)
(<class '__main__.B'>, <class '__main__.A'>, <type 'object'>)

How to get base type for the particular type

  1. ITypeSymbol.BaseType is exactly that you need to retrieve the base type. BaseType can be null if your type is System.Object, interface or pointer type, also if you have a couple of troubles with semantic logic in your Compilation it can be a error type, which mean you miss something reference and Roslyn cannot corectly resolve a types. In other case symbol.BaseType should work very well by default. So I sugges you to check your symbol and check diagnostics in Compilation

  2. ITypeSymbol.ToString() will return string that would be constructed by CSharpErrorMessageFormat, which has FQN type style that is enough for your question. If you would a more, you can pass a custom SymbolDisplayFormat to ToDisplayString

  3. For declaration nodes SemanticModel.GetTypeInfo will returns the null TypeSymbol and ConvertedSymbol by design, instead of this you should use SemanticModel.GetDeclaredSymbol

    var propertyTypeSymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclaration) as ITypeSymbol;
    var classTypeSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration) as ITypeSymbol;

    P.S. be carefull, to use GetDeclaredSymbol for some special declaration nodes: for example FieldDeclarationSyntax)

Get base class hierarchy methods with IMetaDataImport EnumMethods

GetTypeProps will return the metadata token of the base type in ptkExtends, you can use that to walk up the inheritance tree and collect the methods from each as you go.

Be aware, however, that the metadata token might not be a TypeDef. It could be a TypeRef (requiring you to resolve the type) or a TypeSpec (requiring you to parse the type signature and extract an appropriate TypeDef/TypeRef).

Detect common base class

There was at some point bases and direct_bases in std::tr2 but that wasn't included. Some versions of gcc have it. Using these perhaps you can get what you want.

How do you get the all properties of a class and its base classes (up the hierarchy) with Reflection? (C#)

Use this:

PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

EDIT: Of course the correct answer is that of Jay. GetProperties() without parameters is equivalent to GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static ). The BindingFlags.FlattenHierarchy plays no role here.

Find all parent types (both base classes and interfaces)

More general solution:

public static bool InheritsFrom(this Type type, Type baseType)
{
// null does not have base type
if (type == null)
{
return false;
}

// only interface or object can have null base type
if (baseType == null)
{
return type.IsInterface || type == typeof(object);
}

// check implemented interfaces
if (baseType.IsInterface)
{
return type.GetInterfaces().Contains(baseType);
}

// check all base types
var currentType = type;
while (currentType != null)
{
if (currentType.BaseType == baseType)
{
return true;
}

currentType = currentType.BaseType;
}

return false;
}

Or to actually get all parent types:

public static IEnumerable<Type> GetParentTypes(this Type type)
{
// is there any base type?
if (type == null)
{
yield break;
}

// return all implemented or inherited interfaces
foreach (var i in type.GetInterfaces())
{
yield return i;
}

// return all inherited types
var currentBaseType = type.BaseType;
while (currentBaseType != null)
{
yield return currentBaseType;
currentBaseType= currentBaseType.BaseType;
}
}

Visual Studio: How do I show all classes inherited from a base class?

Sure, Resharper can do this. And much more.

Just right click on type name in any place and choose "Go To Inheritor" in context menu.
"Go To Inheritor" can be also applied to method for navigating to overrides and an interface method's implementations. For an interface you could call "Find Usages Advanced" again, just right click) where to find all extendings and implementations. For a type - derived types.
And my favorite feature - click with holding Control on any type/method for navigating to its declaration.

I think it's a must-have tool for .net developers.


In Resharper 9.2, on any type in source code, rt-click "Find Usage Advanced", select Find="Derived" and Scope="Solutions and Libraries".

For example, to find all inheritors (both in the library and your code) of some base class in an included DLL from any vendor, declare a variable in your code with that base class. Then right-click on that base class name you just typed.



Related Topics



Leave a reply



Submit