TypeTree

Builds and caches type inheritance hierarchies using Unity's TypeCache. Each node in the tree represents a type, with children being its direct subclasses. Supports enumeration over the full subtree and implicit conversion to Type.

Definition

Namespace: Paragon.Editor Assembly: Paragon.Editor.dll

public class TypeTree : IEnumerable<Type>

Implements: IEnumerable<Type>

Remarks

TypeTree provides a structured view of type inheritance hierarchies, built on top of Unity's TypeCache.GetTypesDerivedFrom(). Unlike TypeCache, which returns a flat list of derived types, TypeTree organizes them into a parent-child tree that mirrors the actual inheritance chain.

Caching

All TypeTree instances are cached in a static Dictionary<Type, TypeTree>. Calling GetTypeTree() with the same root type returns the cached instance. Subtrees are also cached — if you build a tree for BaseClass, then later request DerivedClass, the existing node is reused.

Generic Type Handling

When resolving the parent type, generic types are normalized to their generic type definition. For example, MyClass<int> maps to MyClass<> as its parent, ensuring the tree structure is consistent regardless of generic type arguments.

Enumeration

TypeTree implements IEnumerable<Type> with a depth-first pre-order traversal. Iterating yields the root type first, then recursively yields all descendants:

Quick Lookup

Goal
How

Build type hierarchy

TypeTree tree = TypeTree.GetTypeTree(typeof(MyBase))

Get the type

tree.Type or implicit Type t = tree

Get parent node

tree.Parent

Get direct children

tree.Children

Iterate all descendants

foreach (Type t in tree) { ... }

Use with LINQ

tree.Where(t => !t.IsAbstract)

Properties

Property
Type
Access
Description

Type

Type

public

The System.Type this node represents

Parent

TypeTree

public

The parent node in the tree (looked up from the static cache)

Children

IEnumerable<TypeTree>

public

Direct child nodes (immediate subclasses)

Fields

Field
Type
Access
Description

typeTrees

Dictionary<Type, TypeTree>

private static readonly

Global cache of all TypeTree nodes

type

Type

private readonly

The type this node represents

children

HashSet<TypeTree>

private readonly

Direct child nodes

parent

Type

private readonly

The parent type (not the node — resolved via cache)

Constructors

TypeTree(Type) — private

Creates a new node, resolves the parent type, registers itself in the global cache, and initializes an empty children set.

Methods

GetTypeTree

Builds (or retrieves from cache) a full type hierarchy rooted at the specified type.

Parameter
Type
Description

root

Type

The root type to build the hierarchy from

Returns: The TypeTree node for the root type, with all descendants populated.

Algorithm:

  1. Checks the cache — returns immediately if found

  2. Creates a new TypeTree node for the root

  3. Gets all derived types via TypeCache.GetTypesDerivedFrom(root)

  4. For each derived type, gets or creates a TypeTree node

  5. Links each child node to its parent node's children set

GetParentType (private static)

Resolves the parent type, normalizing generic types to their generic type definition.

GetEnumerator

Depth-first pre-order traversal: yields the current node's type, then recursively yields all children.

Operators

Implicitly converts a TypeTree to its underlying Type.

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation

Examples

Building a Type Hierarchy

Iterating All Descendants

Implicit Type Conversion

See Also

Last updated