Table of Contents

Class TypeEx

Namespace
Utils.Reflection
Assembly
Utils.dll

Provides extension methods for various Type reflection operations.

public static class TypeEx
Inheritance
TypeEx
Inherited Members

Methods

GetGenericTypeDefinitionFor(Type, Type)

Returns the generic type arguments for the specified baseType that the current type is derived from.

public static Type[] GetGenericTypeDefinitionFor(this Type type, Type baseType)

Parameters

type Type

The type being checked.

baseType Type

The generic base type definition.

Returns

Type[]

An array of generic type arguments if found; otherwise, null.

GetPropertiesOrFields(Type)

Gets all properties and fields of the specified type.

public static PropertyOrFieldInfo[] GetPropertiesOrFields(this Type type)

Parameters

type Type

The type whose properties and fields to retrieve.

Returns

PropertyOrFieldInfo[]

An array of PropertyOrFieldInfo representing all properties and fields of the type.

GetPropertiesOrFields(Type, BindingFlags)

Gets all properties and fields of the specified type using the specified binding flags.

public static PropertyOrFieldInfo[] GetPropertiesOrFields(this Type type, BindingFlags bindingFlags)

Parameters

type Type

The type whose properties and fields to retrieve.

bindingFlags BindingFlags

A combination of BindingFlags to control the search.

Returns

PropertyOrFieldInfo[]

An array of PropertyOrFieldInfo representing all properties and fields of the type.

GetPropertyOrField(Type, string)

Gets a property or field by name from the specified type.

public static PropertyOrFieldInfo? GetPropertyOrField(this Type type, string name)

Parameters

type Type

The type from which to retrieve the property or field.

name string

The name of the property or field.

Returns

PropertyOrFieldInfo

A PropertyOrFieldInfo representing the found member, or null if no matching member exists.

Exceptions

AmbiguousMatchException

Thrown when the lookup resolves to more than one member (for example, a property and a field with the same name in different scopes, or a hidden member in a derived class).

GetPropertyOrField(Type, string, BindingFlags)

Gets a property or field by name from the specified type, using the given binding flags.

public static PropertyOrFieldInfo? GetPropertyOrField(this Type type, string name, BindingFlags bindingFlags)

Parameters

type Type

The type from which to retrieve the property or field.

name string

The name of the property or field.

bindingFlags BindingFlags

A combination of BindingFlags to control the search.

Returns

PropertyOrFieldInfo

A PropertyOrFieldInfo representing the found member, or null if no matching member exists.

Exceptions

AmbiguousMatchException

Thrown when the lookup resolves to more than one member under the given bindingFlags.

GetStaticMethod(Type, Type[], string, Type[])

Gets the public static method named methodName with the exact argumentTypes from the closed generic type obtained by applying genericTypeArguments to type.

public static MethodInfo? GetStaticMethod(this Type type, Type[] genericTypeArguments, string methodName, Type[] argumentTypes)

Parameters

type Type

An open generic type definition (e.g. typeof(List<>)).

genericTypeArguments Type[]

Type arguments to close the generic definition.

methodName string

Name of the static method to retrieve.

argumentTypes Type[]

Exact parameter types of the method to retrieve.

Returns

MethodInfo

The matching MethodInfo, or null when no method with the given name and parameter types exists.

Exceptions

ArgumentException

Thrown when type is not an open generic type definition, or when the number of genericTypeArguments does not match the type's arity.

GetStaticMethods(Type, Type[], string)

Gets all public static methods named methodName from the closed generic type obtained by applying genericTypeArguments to type.

public static MethodInfo[] GetStaticMethods(this Type type, Type[] genericTypeArguments, string methodName)

Parameters

type Type

An open generic type definition (e.g. typeof(List<>)).

genericTypeArguments Type[]

Type arguments to close the generic definition.

methodName string

Name of the static methods to retrieve.

Returns

MethodInfo[]

An array of matching MethodInfo instances; empty when none match.

Exceptions

ArgumentException

Thrown when type is not an open generic type definition, or when the number of genericTypeArguments does not match the type's arity.

GetUnderlyingType(Type)

Gets the underlying type for the given type. If the type is an enum, it returns the enum's underlying type. If the type is nullable, it returns the underlying non-nullable type.

public static Type GetUnderlyingType(this Type type)

Parameters

type Type

The type whose underlying type to retrieve.

Returns

Type

The underlying type, or the original type if it is not an enum or nullable.

IsAssignableFromEx(Type, Type)

Checks if toAssign can be implicitly assigned to a variable of type toBeAssigned.

public static bool IsAssignableFromEx(this Type toBeAssigned, Type toAssign)

Parameters

toBeAssigned Type

Target type of the assignment.

toAssign Type

Source type of the assignment.

Returns

bool

true if a C# implicit conversion exists from toAssign to toBeAssigned, or if the CLR considers toAssign directly assignable to toBeAssigned.

Remarks

For numeric types, only the implicit numeric conversions defined by the C# specification are accepted. uint → int or long → int are therefore rejected even though they share the same storage size. Use IsAssignableFrom(Type) when CLR assignability — rather than C# implicit conversion — is the intended check.

IsDefinedBy(Type, Type)

Determines whether the specified <code class="paramref">type</code> is derived from, implements, 
or is the same as the <code class="paramref">baseType</code>. This includes handling for generic 
type definitions (e.g. 
List<>

).

public static bool IsDefinedBy(this Type type, Type baseType)

Parameters

type Type

The concrete type to check.

baseType Type

The base or interface type to check against. May be generic.

Returns

bool

true if type is or inherits from baseType, or implements baseType (if it's an interface), including handling for generic type definitions; otherwise, false.

Exceptions

ArgumentNullException

Thrown if either type or baseType is null.