Class TypeExtensions
- Namespace
- Garyon.Reflection
- Assembly
- Garyon.dll
Provides extension methods for the Type class.
public static class TypeExtensions
- Inheritance
-
TypeExtensions
- Inherited Members
Methods
CanBeInherited(Type)
Determines whether the provided type can be inherited.
public static bool CanBeInherited(this Type type)
Parameters
typeTypeThe type to determine whether it can be inherited.
Returns
- bool
A value determining whether
typecan be inherited; that is, a non-sealed, thus non-static, class or an interface type.
CanConstruct(Type, Type)
Determines whether the specified base generic type can be constructed into a type.
public static bool CanConstruct(this Type genericType, Type constructedType)
Parameters
genericTypeTypeThe base generic type.
constructedTypeTypeThe constructed type to determine whether it is constructed from the provided generic type.
Returns
Exceptions
- InvalidOperationException
The
genericTypeargument is not an unbound generic type or theconstructedTypeargument is not a bound generic type.
CanConstruct<T>(Type)
Determines whether the specified base generic type can be constructed into a type.
public static bool CanConstruct<T>(this Type genericType)
Parameters
genericTypeTypeThe base generic type.
Returns
Type Parameters
TThe type to determine whether it can be a construction of the base generic type.
Exceptions
- InvalidOperationException
The
genericTypeargument is not an unbound generic type or theTargument is not a bound generic type.
CanInherit(Type)
Determines whether the provided type can inherit any type, either a class or an interface.
public static bool CanInherit(this Type type)
Parameters
typeTypeThe type to determine whether it can inherit any type.
Returns
- bool
true if the type can inherit any type; that is, it is not a pointer or a by reference type, otherwise false.
CanInheritClasses(Type)
Determines whether the provided type can inherit any class.
public static bool CanInheritClasses(this Type type)
Parameters
typeTypeThe type to determine whether it can inherit any class.
Returns
- bool
true if the type can inherit any interface; that is, it is not a pointer, a by reference, or a delegate type, otherwise false.
CanInheritCustomClasses(Type)
Determines whether the provided type can inherit any custom class.
public static bool CanInheritCustomClasses(this Type type)
Parameters
typeTypeThe type to determine whether it can inherit any custom class.
Returns
- bool
true if the type can inherit any interface; that is, it is not a pointer, a by reference, a static class, or a delegate type, otherwise false.
CanInheritCustomTypes(Type)
Determines whether the provided type can inherit any type, either a class or an interface.
public static bool CanInheritCustomTypes(this Type type)
Parameters
typeTypeThe type to determine whether it can inherit any type.
Returns
- bool
true if the type can inherit any type; that is, it is not a pointer, a by reference, a static class, a delegate, an enum or an array type, otherwise false.
CanInheritInterfaces(Type)
Determines whether the provided type can inherit any interface.
public static bool CanInheritInterfaces(this Type type)
Parameters
typeTypeThe type to determine whether it can inherit any interface.
Returns
- bool
true if the type can inherit any interface; that is, it is not a pointer, a by reference, a static class, a delegate, an enum or an array type, otherwise false.
ContainsElementsOfType(Type, Type)
Determines whether the provided type contains elements of type
elementType.
public static bool ContainsElementsOfType(this Type type, Type elementType)
Parameters
typeTypeThe type.
elementTypeTypeThe type of the elements that the provided type should contain.
Returns
- bool
A value determining whether the provided type contains elements of type
elementType.
ContainsElementsOfType<T>(Type)
Determines whether the provided type contains elements of type
T.
public static bool ContainsElementsOfType<T>(this Type type)
Parameters
typeTypeThe type.
Returns
- bool
A value determining whether the provided type contains elements of type
T.
Type Parameters
TThe type of the elements that the provided type should contain.
EnumerateBaseTypes(Type)
Enumerates all base types of a type.
public static IEnumerable<Type> EnumerateBaseTypes(this Type type)
Parameters
typeType
Returns
- IEnumerable<Type>
A lazily evaluated collection of types, with the first being the directly inherited base type, and the last being the furthest in the inheritance tree.
Remarks
This only includes all types returned from BaseType recursively. It does not include implemented interfaces.
GenericFullNamePrefixOrSame(Type)
Gets the full name prefix of a generic type ignoring its arity, or the same full name if the type is not a generic type.
public static string? GenericFullNamePrefixOrSame(this Type type)
Parameters
typeTypeThe type whose full name prefix to get.
Returns
GetAccessibilityModifiers(Type?)
public static AccessibilityModifiers GetAccessibilityModifiers(this Type? type)
Parameters
typeType
Returns
GetAnyAccessibilityConstructor(Type, params Type[])
Gets the constructor with the specified argument types of the Type. The constructor may have any accessibility.
public static ConstructorInfo? GetAnyAccessibilityConstructor(this Type type, params Type[] argumentTypes)
Parameters
typeTypeThe type whose constructor to get.
argumentTypesType[]The argument types of the constructor to get.
Returns
- ConstructorInfo
The constructor of the type, or null if the type does not have one.
GetAnyAccessibilityConstructor<T1>(Type)
Gets the constructor of the Type that has 1 argument and whose argument types are in the order they are provided in the type arguments. The constructor may have any accessibility.
public static ConstructorInfo? GetAnyAccessibilityConstructor<T1>(this Type type)
Parameters
typeTypeThe type whose constructor to get.
Returns
- ConstructorInfo
The constructor of the type, or null if the type does not have one.
Type Parameters
T1The type of the 1st argument of the constructor to get.
GetAnyAccessibilityConstructor<T1, T2>(Type)
Gets the constructor of the Type that has 2 arguments and whose argument types are in the order they are provided in the type arguments. The constructor may have any accessibility.
public static ConstructorInfo? GetAnyAccessibilityConstructor<T1, T2>(this Type type)
Parameters
typeTypeThe type whose constructor to get.
Returns
- ConstructorInfo
The constructor of the type, or null if the type does not have one.
Type Parameters
T1The type of the 1st argument of the constructor to get.
T2The type of the 2nd argument of the constructor to get.
GetAnyAccessibilityConstructor<T1, T2, T3>(Type)
Gets the constructor of the Type that has 3 arguments and whose argument types are in the order they are provided in the type arguments. The constructor may have any accessibility.
public static ConstructorInfo? GetAnyAccessibilityConstructor<T1, T2, T3>(this Type type)
Parameters
typeTypeThe type whose constructor to get.
Returns
- ConstructorInfo
The constructor of the type, or null if the type does not have one.
Type Parameters
T1The type of the 1st argument of the constructor to get.
T2The type of the 2nd argument of the constructor to get.
T3The type of the 3rd argument of the constructor to get.
GetAnyAccessibilityConstructor<T1, T2, T3, T4>(Type)
Gets the constructor of the Type that has 4 arguments and whose argument types are in the order they are provided in the type arguments. The constructor may have any accessibility.
public static ConstructorInfo? GetAnyAccessibilityConstructor<T1, T2, T3, T4>(this Type type)
Parameters
typeTypeThe type whose constructor to get.
Returns
- ConstructorInfo
The constructor of the type, or null if the type does not have one.
Type Parameters
T1The type of the 1st argument of the constructor to get.
T2The type of the 2nd argument of the constructor to get.
T3The type of the 3rd argument of the constructor to get.
T4The type of the 4th argument of the constructor to get.
GetAnyAccessibilityParameterlessConstructor(Type)
Gets the parameterless constructor of the Type. The constructor may have any accessibility.
public static ConstructorInfo? GetAnyAccessibilityParameterlessConstructor(this Type type)
Parameters
typeTypeThe type whose parameterless constructor to get.
Returns
- ConstructorInfo
The parameterless constructor of the type, or null if the type does not have one.
GetArrayJaggingLevel(Type)
Gets the array jagging level of the provided array type.
public static int GetArrayJaggingLevel(this Type type)
Parameters
typeTypeThe jagged array type.
Returns
- int
The jagging level of the jagged array, if the provided type is an array, otherwise 0.
GetConstructor(Type, params Type[])
Gets the constructor with the specified argument types of the Type.
public static ConstructorInfo? GetConstructor(this Type type, params Type[] argumentTypes)
Parameters
typeTypeThe type whose constructor to get.
argumentTypesType[]The argument types of the constructor to get.
Returns
- ConstructorInfo
The constructor of the type, or null if the type does not have one.
GetConstructor<T1>(Type)
Gets the constructor of the Type that has 1 argument and whose argument types are in the order they are provided in the type arguments.
public static ConstructorInfo? GetConstructor<T1>(this Type type)
Parameters
typeTypeThe type whose constructor to get.
Returns
- ConstructorInfo
The constructor of the type, or null if the type does not have one.
Type Parameters
T1The type of the 1st argument of the constructor to get.
GetConstructor<T1, T2>(Type)
Gets the constructor of the Type that has 2 arguments and whose argument types are in the order they are provided in the type arguments.
public static ConstructorInfo? GetConstructor<T1, T2>(this Type type)
Parameters
typeTypeThe type whose constructor to get.
Returns
- ConstructorInfo
The constructor of the type, or null if the type does not have one.
Type Parameters
T1The type of the 1st argument of the constructor to get.
T2The type of the 2nd argument of the constructor to get.
GetConstructor<T1, T2, T3>(Type)
Gets the constructor of the Type that has 3 arguments and whose argument types are in the order they are provided in the type arguments.
public static ConstructorInfo? GetConstructor<T1, T2, T3>(this Type type)
Parameters
typeTypeThe type whose constructor to get.
Returns
- ConstructorInfo
The constructor of the type, or null if the type does not have one.
Type Parameters
T1The type of the 1st argument of the constructor to get.
T2The type of the 2nd argument of the constructor to get.
T3The type of the 3rd argument of the constructor to get.
GetConstructor<T1, T2, T3, T4>(Type)
Gets the constructor of the Type that has 4 arguments and whose argument types are in the order they are provided in the type arguments.
public static ConstructorInfo? GetConstructor<T1, T2, T3, T4>(this Type type)
Parameters
typeTypeThe type whose constructor to get.
Returns
- ConstructorInfo
The constructor of the type, or null if the type does not have one.
Type Parameters
T1The type of the 1st argument of the constructor to get.
T2The type of the 2nd argument of the constructor to get.
T3The type of the 3rd argument of the constructor to get.
T4The type of the 4th argument of the constructor to get.
GetDeclaringMethodSafe(Type)
Gets the declaring method of the given type. Only generic method type parameters support a declaring method.
public static MethodBase? GetDeclaringMethodSafe(this Type type)
Parameters
typeTypeThe type whose declaring method to get. It must be a generic method type parameter, otherwise null will be returned.
Returns
- MethodBase
The declaring method for the given generic method type parameter, or null if the type is not a generic method type parameter.
GetDeepestElementType(Type)
Gets the deepest element type of the provided type.
public static Type GetDeepestElementType(this Type type)
Parameters
typeTypeThe type.
Returns
- Type
The deepest element type according to GetElementType().
GetDeepestInterfaceInheritanceLevel(Type)
Gets a the deepest interface inheritance level from the interface inheritance tree of the specified type.
public static int GetDeepestInterfaceInheritanceLevel(this Type type)
Parameters
typeTypeThe type whose deepest interface inheritance level to get.
Returns
- int
The deepest interface inheritance level. For pointers, by reference, delegate and enum types, this is equal to 0. For all the other types it is equal to the height of the interface inheritance tree.
GetDefaultValue(Type)
Gets the default value of the given type.
public static object? GetDefaultValue(this Type type)
Parameters
typeTypeThe type whose default value to get.
Returns
- object
Returns the default value of the type, if it is a value type, by calling its parameterless constructor is called, otherwise null.
GetGenericTypeDefinitionOrSame(Type)
Gets the generic type definition of a generic type.
public static Type GetGenericTypeDefinitionOrSame(this Type type)
Parameters
typeTypeThe type whose generic type definition to get.
Returns
- Type
The generic type definition of the generic type, or the provided type itself if the type is not generic.
GetInheritanceLevel(Type)
Determines the number of base types that the provided type directly inherits, excluding the special classes ValueType, Array, Delegate, Enum and object.
public static int GetInheritanceLevel(this Type type)
Parameters
typeTypeThe type whose inheritance level to determine.
Returns
- int
The number of base types that the provided type inherits. For value types, pointers, arrays, and delegates this is equal to 0, and for reference types it is equal to the number of classes that are recursively directly inherited, excluding the object class.
GetInheritanceTree(Type)
Gets a Tree<T> representing the provided type's inheritance tree.
public static Tree<Type> GetInheritanceTree(this Type type)
Parameters
typeTypeThe type whose inheritance tree to get.
Returns
Remarks
The inheritance tree's root is the provided type, and the base classes are ancestors, which is the opposite than the proposed inheritance tree views, that have the deepest base object as the root, with each child being the inheritor.
GetInterfaceInheritanceTree(Type)
Gets a Tree<T> representing the provided type's interface inheritance tree.
public static Tree<Type> GetInterfaceInheritanceTree(this Type type)
Parameters
typeTypeThe type whose interface inheritance tree to get.
Returns
- Tree<Type>
A Tree<T> representing the provided type's interface inheritance tree. Each level only contains direct interface implementations, meaning that no node has nodes of indirectly inherited interfaces as direct children, they are indirect descendants though.
GetMultiplePointerLevel(Type)
Gets the multiple pointer depth level of the provided pointer type.
public static int GetMultiplePointerLevel(this Type type)
Parameters
typeTypeThe pointer type.
Returns
- int
The depth level of the multiple pointer, if the provided type is a pointer, otherwise 0.
GetOriginalDeclaringGenericMember(Type)
Gets the member that originally declares the given generic parameter type.
public static MemberInfo GetOriginalDeclaringGenericMember(this Type type)
Parameters
typeTypeThe generic type parameter whose original declaring member to get.
Returns
- MemberInfo
The MemberInfo instance representing the member that originally declares the given generic parameter.
Remarks
This method does not work for local generic methods.
Exceptions
- ArgumentException
Thrown when
typeis not a generic type parameter.
GetParameterlessConstructor(Type)
Gets the public parameterless constructor of the Type.
public static ConstructorInfo? GetParameterlessConstructor(this Type type)
Parameters
typeTypeThe type whose public parameterless constructor to get.
Returns
- ConstructorInfo
The public parameterless constructor of the type, or null if the type does not have one, or if the parameterless constructor is not public.
GetTypeCode(Type)
Gets the underlying type code of the specified Type.
public static TypeCode GetTypeCode(this Type type)
Parameters
typeTypeThe type whose underlying type code to get.
Returns
Implements(Type, Type)
Determines whether a type implements an interface type.
public static bool Implements(this Type type, Type interfaceType)
Parameters
typeTypeThe type to check if it implements the provided interface type. Must not be null.
interfaceTypeTypeThe type of the interface. Must not be null.
Returns
Exceptions
- ArgumentException
typeis a type that cannot implement interfaces, orinterfaceTypeis not an interface.
Implements<T>(Type)
Determines whether a type implements an interface type.
public static bool Implements<T>(this Type type) where T : class
Parameters
Returns
Type Parameters
TThe type of the interface.
Exceptions
- ArgumentException
typeis a type that cannot implement interfaces, orTis not an interface.
Inherits(Type, Type)
Determines whether a type inherits another type.
public static bool Inherits(this Type type, Type inherited)
Parameters
typeTypeThe type to determine whether it inherits the type
inherited.inheritedTypeThe type to check whether it is being inherited by the provided
type.
Returns
- bool
A value determining whether
typeinheritsinherited.
InheritsGenericVariantOf(Type, Type, out Type?)
Determines whether a type inherits a generic variant of another type. Supports both classes and interfaces.
public static bool InheritsGenericVariantOf(Type type, Type genericDefinition, out Type? genericVariantType)
Parameters
typeTypeThe type that must inherit the target generic type.
genericDefinitionTypeThe generic definition that must be inherited.
genericVariantTypeTypeThe constructed variant that is inherited. null if it doesn't exist.
Returns
- bool
true if
typeinheritsgenericVariantType, which is a construction ofgenericDefinition, otherwise false.
Exceptions
- ArgumentException
Thrown if the
genericDefinitionargument is not a generic type definition, that is it must not be a constructed generic type.
InheritsOrEquals(Type, Type)
Determines whether a type inherits or equals another type.
public static bool InheritsOrEquals(this Type type, Type inherited)
Parameters
typeTypeThe type to determine whether it inherits or equals the type
inherited.inheritedTypeThe type to check whether it is being inherited by or equal to the provided
type.
Returns
- bool
A value determining whether
typeinherits or equalsinherited.
InheritsOrEquals<T>(Type)
Determines whether a type inherits another type, or is equal to it.
public static bool InheritsOrEquals<T>(this Type type)
Parameters
typeTypeThe type to determine whether it inherits or is equal to the type
T.
Returns
- bool
A value determining whether
typeinherits or equalsT.
Type Parameters
TThe type to check whether it is being inherited by or equal to the provided
type.
Inherits<T>(Type)
Determines whether a type inherits another type.
public static bool Inherits<T>(this Type type)
Parameters
typeTypeThe type to determine whether it inherits the type
T.
Returns
- bool
A value determining whether
typeinheritsT.
Type Parameters
TThe type to check whether it is being inherited by the provided
type.
InitializeInstance(Type)
Initializes a new instance of a Type by calling its public parameterless constructor.
public static object? InitializeInstance(this Type type)
Parameters
typeTypeThe type of the resulting instance.
Returns
InitializeInstance(Type, params object?[]?)
Initializes a new instance of a Type with the given parameters in the constructor.
public static object? InitializeInstance(this Type type, params object?[]? parameters)
Parameters
typeTypeThe type of the resulting instance.
parametersobject[]The parameters of the constructor.
Returns
InitializeInstance<T>(Type)
Initializes a new instance of a Type by calling its public parameterless constructor.
public static T? InitializeInstance<T>(this Type type)
Parameters
typeTypeThe type of the resulting instance.
Returns
- T
The initialized instance, or null if no public parameterless constructor was not found.
Type Parameters
TThe type of the resulting instance.
InitializeInstance<T>(Type, params object?[]?)
Initializes a new instance of a Type with the given parameters in the constructor.
public static T? InitializeInstance<T>(this Type type, params object?[]? parameters)
Parameters
typeTypeThe type of the resulting instance.
parametersobject[]The parameters of the constructor.
Returns
- T
The initialized instance, or null if such a constructor was not found.
Type Parameters
TThe type of the resulting instance.
IsAttribute(Type)
Determines whether the type is an attribute.
public static bool IsAttribute(this Type type)
Parameters
typeTypeThe type to determine whether it is an attribute.
Returns
- bool
A value determining whether the type is an attribute.
IsDelegate(Type)
Determines whether the type is a delegate.
public static bool IsDelegate(this Type type)
Parameters
typeTypeThe type to determine whether it is a delegate.
Returns
- bool
A value determining whether the type is a delegate.
IsException(Type)
Determines whether the type is an exception.
public static bool IsException(this Type type)
Parameters
typeTypeThe type to determine whether it is an exception.
Returns
- bool
A value determining whether the type is an exception.
IsGenericVariantOf(Type, Type)
Determines whether the provided type is a variant of the specified base generic type.
public static bool IsGenericVariantOf(this Type type, Type genericType)
Parameters
typeTypeThe type to determine whether it is a generic variant.
genericTypeTypeThe base generic type.
Returns
Exceptions
- InvalidOperationException
The
genericTypeargument is not an unbound generic type.
IsGenericVariantOrDefinition(Type, Type)
Determines whether the provided type is a variant of the specified base generic type, or a definition that is equal to the provided generic type definition.
public static bool IsGenericVariantOrDefinition(this Type type, Type genericType)
Parameters
typeTypeThe type to determine whether it is a generic variant.
genericTypeTypeThe base generic type.
Returns
Exceptions
- InvalidOperationException
The
genericTypeargument is not an unbound generic type.
IsNullableType(Type)
Determines whether the type is a nullable type; that is, it is a Nullable<T>, a pointer type or a reference type.
public static bool IsNullableType(this Type type)
Parameters
typeTypeThe type to determine whether it is a nullable type.
Returns
IsNullableValueType(Type)
Determines whether the type is a nullable value type; that is, it is a Nullable<T> or a pointer type.
public static bool IsNullableValueType(this Type type)
Parameters
typeTypeThe type to determine whether it is a nullable value type.
Returns
IsOrImplements(Type, Type)
Determines whether the given type is a specific target interface type, or if it implements it
public static bool IsOrImplements(this Type source, Type target)
Parameters
Returns
Exceptions
- ArgumentException
sourceortargetis not an interface type, orsourcecannot implement interfaces.
IsReferenceType(Type)
Determines whether the type is a reference type; that is, not a value type, or a pointer type.
public static bool IsReferenceType(this Type type)
Parameters
typeTypeThe type to determine whether it is a reference type.
Returns
IsStaticClass(Type)
Determines whether the type is a static class.
public static bool IsStaticClass(this Type type)
Parameters
typeTypeThe type to determine whether it is a static class.
Returns
- bool
A value determining whether the type is a static class.
IsTrueClass(Type)
Determines whether the type is an actual class, and not an array, a delegate, a by reference, or a pointer type.
public static bool IsTrueClass(this Type type)
Parameters
typeTypeThe type to determine whether it is a class.
Returns
- bool
A value determining whether the type is a class.
IsTuple(Type)
Determines whether the type is a tuple; that is, any generic variant of the ValueTuple struct, nullable or not.
public static bool IsTuple(this Type type)
Parameters
typeTypeThe type to determine whether it is a tuple.
Returns
- bool
A value determining whether the type is a tuple.
IsValidTypeArgument(Type)
Determines whether the given type is a valid type argument.
public static bool IsValidTypeArgument(this Type type)
Parameters
typeTypeThe type to determine whether it is a valid type argument.
Returns
- bool
true if the type is a valid type argument, that is, it is not a by ref, a by ref-like, a pointer, or void, otherwise false.
IsVoid(Type)
Determines whether the type is void.
public static bool IsVoid(this Type type)
Parameters
Returns
MakeJaggedArrayType(Type, int)
Creates a jagged array type object with the specified jagging level.
public static Type MakeJaggedArrayType(this Type type, int jaggingLevel)
Parameters
typeTypeThe type of the elements within the array.
jaggingLevelintThe jagging level of the resulting jagged array type.
Returns
MakeJaggedArrayType(Type, params int[])
Creates a jagged array type object with the specified ranks for each jag.
public static Type MakeJaggedArrayType(this Type type, params int[] ranks)
Parameters
typeTypeThe type of the elements within the array.
ranksint[]The ranks for each jagging level of the array type. The order is preserved like this: { 1, 2, 3 } => T[][,][,,].
Returns
MakeMultiplePointerType(Type, int)
Creates a multiple pointer type object with the specified pointer depth level.
public static Type MakeMultiplePointerType(this Type type, int pointerDepthLevel)
Parameters
typeTypeThe type of the elements within the pointer.
pointerDepthLevelintThe depth level of the resulting pointer type.