How to get ancestors and descendants of a type declaration using DXCore
Once you obtained an instance of a class, structure or an interface, for example, by using the DXCore SourceModel service:
CodeRush.Source.ActiveClass CodeRush.Source.ActiveClassInterfaceOrStruct CodeRush.Source.ActiveClassInterfaceStructOrModule CodeRush.Source.ActiveStruct CodeRush.Source.ActiveInterface
and would like to analyze its hierarchy by retrieving its base and/or derived types, you can use the following APIs:
- To get the base types:
ITypeElement baseTypes = CodeRush.Source.GetBaseTypes(ITypeElement)
Returns an array of all base types for the given type declaration instance.
ITypeElement baseType = classInstance.GetBaseType();
Returns the next base type for the type instance.
ITypeElement baseTypes = classInstance.GetBaseTypes();
Returns an array of all base types for the type instance.
- To get the derived types:
ITypeElement descendants = classInstance.GetDescendants();
Returns an array of types that derive from the type instance.
- To check if a type descends from another, or is equal to another type:
Boolean descendsFrom = classInstance.DescendsFrom(); Boolean descendsFromOrEqual = classInstance.Is();
The ITypeElement instance represents a type declaration, such as Class, Struct, Interface, Delegate, Enumeration, Module (VB), Union (C++). So, you can cast any type classes to the ITypeElement type and use the methods mentioned above.—– Products: DXCore Versions: 11.1 and up VS IDEs: any Updated: Aug/29/2011 ID: D108