TypeExtensions

Extension methods for System.Type and System.Object that provide enhanced type checking with generic type definition support, and method lookup by name. Used throughout the Paragon framework for reflection-based systems.

Definition

Namespace: Paragon Assembly: Paragon.dll

public static class TypeExtensions

Quick Lookup

Goal
Method

Get type name without generics

type.GetNonGenericName()

Find methods by name

type.GetMethods("MethodName", flags)

Check type assignability (including open generics)

type.IsOfType(targetType)

Check type assignability (generic)

type.IsOfType<IMyInterface>()

Check object's type assignability

obj.IsOfType(targetType)

Check object's type (generic)

obj.IsOfType<IMyInterface>()

Methods

GetNonGenericName

Returns the type name stripped of the generic arity suffix (the backtick and digit, e.g., `2).

public static string GetNonGenericName(this Type type)

Returns: The name without the generic parameter count. For example, List1List, Dictionary2Dictionary.

GetMethods (by name)

Returns all methods on a type that match the given name, filtered by binding flags.

Parameter
Type
Description

type

Type

The type to search

name

string

Method name to filter by

bindingFlags

BindingFlags

Binding flags for the search. Defaults to BindingFlags.Default.

Returns: All MethodInfo objects with the matching name.

circle-info

This is useful for finding method overloads — GetMethod() throws for ambiguous matches, but GetMethods(name) returns all of them for further filtering.

IsOfType (Type, Type)

Checks whether a type is assignable to a target type, with full support for open generic type definitions. This is the core type-checking method used across the Paragon framework.

Parameter
Type
Description

type

Type

The type to check

target

Type

The target type to check against

Returns: true if type matches or is assignable to target.

Resolution order:

  1. Exact type equality (type == target)

  2. For non-generic targets: target.IsAssignableFrom(type) — standard .NET assignability

  3. For open generic definitions: checks the type itself, all implemented interfaces, and the entire base type chain for matching generic type definitions

IsOfType<TType> (Type)

Generic convenience overload.

IsOfType (object, Type)

Checks whether an object's runtime type is assignable to the target type. Returns false for null objects.

Parameter
Type
Description

@object

object

The object to check (null-safe)

target

Type

The target type

Returns: true if the object is non-null and its type matches.

IsOfType<TType> (object)

Generic convenience overload for object type checking.

Open Generic Support

Standard .NET IsAssignableFrom() does not handle open generic type definitions. IsOfType() fills this gap:

The algorithm checks three sources for open generic matches:

  1. The type itself — is type a closed version of the target's generic definition?

  2. All interfaces — does type implement an interface that is a closed version of the target?

  3. The base type chain — does any ancestor of type match the target's generic definition?

Common Pitfalls

circle-exclamation

GetNonGenericName on non-generic types For non-generic types, Split('')` returns the full name unchanged (since there is no backtick). This is safe but the method name may be misleading when applied to non-generic types. {% endhint %}

See Also

Last updated