Version 3.3.1.2

TypeExtensions class

Namespace: Sirenix.Utilities
Assembly: Sirenix.Utilities
public static class TypeExtensions
Type method extensions.
Inheritance
  • System.Object
  • TypeExtensions

Fields

TypeNameAlternatives
Type name alias lookup.
public static readonly Dictionary<Type, string> TypeNameAlternatives

Methods

AreGenericConstraintsSatisfiedBy(MethodBase, Type[])

Checks whether an array of types satisfy the constraints of a given generic method definition.

If this method returns true, the given parameters can be safely used with System.Reflection.MethodInfo.MakeGenericMethod(System.Type[]) with the given generic method definition.

public static bool AreGenericConstraintsSatisfiedBy(this MethodBase genericMethod, params Type[] parameters)
Parameters
System.Reflection.MethodBase genericMethod

The generic method definition to check.

System.Type[] parameters

The parameters to check validity for.

Returns
System.Boolean

Exceptions
System.ArgumentNullException

genericType is null or types is null

System.ArgumentException

The genericMethod parameter must be a generic method definition.

AreGenericConstraintsSatisfiedBy(Type, Type[])

Checks whether an array of types satisfy the constraints of a given generic type definition.

If this method returns true, the given parameters can be safely used with System.Type.MakeGenericType(System.Type[]) with the given generic type definition.

public static bool AreGenericConstraintsSatisfiedBy(this Type genericType, params Type[] parameters)
Parameters
System.Type genericType

The generic type definition to check.

System.Type[] parameters

The parameters to check validity for.

Returns
System.Boolean

Exceptions
System.ArgumentNullException

genericType is null or types is null

System.ArgumentException

The genericType parameter must be a generic type definition.

AreGenericConstraintsSatisfiedBy(Type[], Type[])
public static bool AreGenericConstraintsSatisfiedBy(Type[] definitions, Type[] parameters)
Parameters
System.Type[] definitions

System.Type[] parameters

Returns
System.Boolean

FindIdealConstructor(Type, BindingFlags)
Weighs multiple constructors for a given type, and attempts to find the most ideal constructor. This will ignore any unmanaged constructors.
public static ConstructorInfo FindIdealConstructor(Type type, BindingFlags flags = BindingFlags.Default)
Parameters
System.Type type

The System.Type to weigh the constructors of.

System.Reflection.BindingFlags flags

The System.Reflection.BindingFlags to search for the constructors; System.Reflection.BindingFlags.Default means only find the public ones.

Returns
System.Reflection.ConstructorInfo

The most ideal System.Reflection.ConstructorInfo based on the scoring system.

GenericArgumentsContainsTypes(Type, Type[])
Determines whether a generic type contains the specified generic argument constraints.
public static bool GenericArgumentsContainsTypes(this Type type, params Type[] types)
Parameters
System.Type type

The type.

System.Type[] types

The generic argument types.

Returns
System.Boolean

GenericParameterIsFulfilledBy(Type, Type)
public static bool GenericParameterIsFulfilledBy(this Type genericParameterDefinition, Type parameterType)
Parameters
System.Type genericParameterDefinition

System.Type parameterType

Returns
System.Boolean

GetAllMembers(Type, BindingFlags)
Gets all members from a given type, including members from all base types if the System.Reflection.BindingFlags.DeclaredOnly flag isn't set.
public static IEnumerable<MemberInfo> GetAllMembers(this Type type, BindingFlags flags = BindingFlags.Default)
Parameters
System.Type type

System.Reflection.BindingFlags flags

Returns
System.Collections.Generic.IEnumerable<System.Reflection.MemberInfo>

GetAllMembers(Type, String, BindingFlags)
Gets all members from a given type, including members from all base types.
public static IEnumerable<MemberInfo> GetAllMembers(this Type type, string name, BindingFlags flags = BindingFlags.Default)
Parameters
System.Type type

System.String name

System.Reflection.BindingFlags flags

Returns
System.Collections.Generic.IEnumerable<System.Reflection.MemberInfo>

GetAllMembers<T>(Type, BindingFlags)
Gets all members of a specific type from a type, including members from all base types, if the System.Reflection.BindingFlags.DeclaredOnly flag isn't set.
public static IEnumerable<T> GetAllMembers<T>(this Type type, BindingFlags flags = BindingFlags.Default)
    where T : MemberInfo
Parameters
System.Type type

System.Reflection.BindingFlags flags

Returns
System.Collections.Generic.IEnumerable<T>

Type Parameters
T

GetArgumentsOfInheritedOpenGenericClass(Type, Type)
Gets the generic arguments of an inherited open generic class.
public static Type[] GetArgumentsOfInheritedOpenGenericClass(this Type candidateType, Type openGenericType)
Parameters
System.Type candidateType

Type of the candidate.

System.Type openGenericType

Type of the open generic class.

Returns
System.Type[]

GetArgumentsOfInheritedOpenGenericInterface(Type, Type)
Gets the generic arguments of an inherited open generic interface.
public static Type[] GetArgumentsOfInheritedOpenGenericInterface(this Type candidateType, Type openGenericInterfaceType)
Parameters
System.Type candidateType

Type of the candidate.

System.Type openGenericInterfaceType

Type of the open generic interface.

Returns
System.Type[]

GetArgumentsOfInheritedOpenGenericType(Type, Type)
Gets the generic arguments of an inherited open generic class or interface.
public static Type[] GetArgumentsOfInheritedOpenGenericType(this Type candidateType, Type openGenericType)
Parameters
System.Type candidateType

Type of the candidate.

System.Type openGenericType

The open generic type to get the arguments of.

Returns
System.Type[]

GetAttribute<T>(Type, Boolean)
Gets the first attribute of type T. Returns null in the no attribute of type T was found.
public static T GetAttribute<T>(this Type type, bool inherit)
    where T : Attribute
Parameters
System.Type type

The type.

System.Boolean inherit

If true, specifies to also search the ancestors of element for custom attributes.

Returns
T

Type Parameters
T

GetBaseClasses(Type, Boolean)
Returns a lazy enumerable of all the base classes of this type
public static IEnumerable<Type> GetBaseClasses(this Type type, bool includeSelf = false)
Parameters
System.Type type

System.Boolean includeSelf

Returns
System.Collections.Generic.IEnumerable<System.Type>

GetBaseTypes(Type, Boolean)
Returns a lazy enumerable of all the base types of this type including interfaces and classes
public static IEnumerable<Type> GetBaseTypes(this Type type, bool includeSelf = false)
Parameters
System.Type type

System.Boolean includeSelf

Returns
System.Collections.Generic.IEnumerable<System.Type>

GetCastMethod(Type, Type, Boolean)
If a type can be casted to another type, this provides the method info of the method in charge of converting the type.
public static MethodInfo GetCastMethod(this Type from, Type to, bool requireImplicitCast = false)
Parameters
System.Type from

From.

System.Type to

To.

System.Boolean requireImplicitCast

if set to true an implicit or explicit operator must be defined on the given type.

Returns
System.Reflection.MethodInfo

GetCastMethodDelegate(Type, Type, Boolean)
If a type can be casted to another type, this provides a function to manually convert the type.
public static Func<object, object> GetCastMethodDelegate(this Type from, Type to, bool requireImplicitCast = false)
Parameters
System.Type from

From.

System.Type to

To.

System.Boolean requireImplicitCast

if set to true an implicit or explicit operator must be defined on the given type.

Returns
System.Func<System.Object, System.Object>

GetCastMethodDelegate<TFrom, TTo>(Boolean)
If a type can be casted to another type, this provides a function to manually convert the type.
public static Func<TFrom, TTo> GetCastMethodDelegate<TFrom, TTo>(bool requireImplicitCast = false)
Parameters
System.Boolean requireImplicitCast

if set to true an implicit or explicit operator must be defined on the given type.

Returns
System.Func<TFrom, TTo>

Type Parameters
TFrom

TTo

GetCompilableNiceFullName(Type)
Gets the full name of the compilable nice.
public static string GetCompilableNiceFullName(this Type type)
Parameters
System.Type type

The type.

Returns
System.String

GetCompilableNiceName(Type)
Gets the name of the compilable nice.
public static string GetCompilableNiceName(this Type type)
Parameters
System.Type type

The type.

Returns
System.String

GetCtorScore(ConstructorInfo)
public static int GetCtorScore(ConstructorInfo ctor)
Parameters
System.Reflection.ConstructorInfo ctor

Returns
System.Int32

GetCustomAttribute<T>(Type)
Returns the first found non-inherited custom attribute of type T on this type Returns null if none was found
public static T GetCustomAttribute<T>(this Type type)
    where T : Attribute
Parameters
System.Type type

Returns
T

Type Parameters
T

GetCustomAttribute<T>(Type, Boolean)
Returns the first found custom attribute of type T on this type Returns null if none was found
public static T GetCustomAttribute<T>(this Type type, bool inherit)
    where T : Attribute
Parameters
System.Type type

System.Boolean inherit

Returns
T

Type Parameters
T

GetCustomAttributes<T>(Type)
Gets all attributes of type T.
public static IEnumerable<T> GetCustomAttributes<T>(this Type type)
    where T : Attribute
Parameters
System.Type type

The type.

Returns
System.Collections.Generic.IEnumerable<T>

Type Parameters
T

GetCustomAttributes<T>(Type, Boolean)
Gets all attributes of type T.
public static IEnumerable<T> GetCustomAttributes<T>(this Type type, bool inherit)
    where T : Attribute
Parameters
System.Type type

The type

System.Boolean inherit

If true, specifies to also search the ancestors of element for custom attributes.

Returns
System.Collections.Generic.IEnumerable<T>

Type Parameters
T

GetEnumBitmask(Object, Type)
Gets the enum bitmask in a ulong.
public static ulong GetEnumBitmask(object value, Type enumType)
Parameters
System.Object value

System.Type enumType

Returns
System.UInt64

Exceptions
System.ArgumentException

enumType

GetEqualityComparerDelegate<T>()
Gets an equality comparer delegate used to compare the equality of values of a given type. In order, this will be: 1. The == operator, if one is defined on the type. 2. A delegate that uses System.IEquatable`1, if the type implements that interface. 3. .NET's own System.Collections.Generic.EqualityComparer`1.Default
public static Func<T, T, bool> GetEqualityComparerDelegate<T>()
Returns
System.Func<T, T, System.Boolean>

Type Parameters
T

GetGenericBaseType(Type, Type)
Gets the generic type definition of an open generic base type.
public static Type GetGenericBaseType(this Type type, Type baseType)
Parameters
System.Type type

System.Type baseType

Returns
System.Type

GetGenericBaseType(Type, Type, out Int32)
Gets the generic type definition of an open generic base type.
public static Type GetGenericBaseType(this Type type, Type baseType, out int depthCount)
Parameters
System.Type type

System.Type baseType

System.Int32 depthCount

Returns
System.Type

GetGenericConstraintsString(Type, Boolean)
Not yet documented.
public static string GetGenericConstraintsString(this Type type, bool useFullTypeNames = false)
Parameters
System.Type type

System.Boolean useFullTypeNames

Returns
System.String

GetGenericParameterConstraintsString(Type, Boolean)
Formats a string with the specified generic parameter constraints on any given type. Example output: where T : class
public static string GetGenericParameterConstraintsString(this Type type, bool useFullTypeNames = false)
Parameters
System.Type type

System.Boolean useFullTypeNames

Returns
System.String

GetInheritanceDistance(Type, Type)
Gets the number of base types between given type and baseType.
public static int GetInheritanceDistance(this Type type, Type baseType)
Parameters
System.Type type

System.Type baseType

Returns
System.Int32

GetMemberValue(MemberInfo, Object)
Gets the value contained in a given System.Reflection.MemberInfo. Currently only System.Reflection.FieldInfo and System.Reflection.PropertyInfo is supported.
public static object GetMemberValue(this MemberInfo member, object obj)
Parameters
System.Reflection.MemberInfo member

The System.Reflection.MemberInfo to get the value of.

System.Object obj

The instance to get the value from.

Returns
System.Object

The value contained in the given System.Reflection.MemberInfo.

Exceptions
System.ArgumentException

Can't get the value of the given System.Reflection.MemberInfo type.

GetNiceFullName(Type)
Returns a nicely formatted full name of a type.
public static string GetNiceFullName(this Type type)
Parameters
System.Type type

Returns
System.String

GetNiceName(Type)
Returns a nicely formatted name of a type.
public static string GetNiceName(this Type type)
Parameters
System.Type type

Returns
System.String

GetOperatorMethod(Type, Operator)
Gets the MethodInfo of a specific operator type.
public static MethodInfo GetOperatorMethod(this Type type, Operator op)
Parameters
System.Type type

Operator op

Returns
System.Reflection.MethodInfo

GetOperatorMethod(Type, Operator, Type, Type)
Gets the MethodInfo of a specific operator kind, with the given left and right operands. This overload is *far* faster than any of the other GetOperatorMethod implementations, and should be used whenever possible.
public static MethodInfo GetOperatorMethod(this Type type, Operator op, Type leftOperand, Type rightOperand)
Parameters
System.Type type

Operator op

System.Type leftOperand

System.Type rightOperand

Returns
System.Reflection.MethodInfo

GetOperatorMethods(Type, Operator)
Gets the MethodInfo of a specific operator type.
public static MethodInfo[] GetOperatorMethods(this Type type, Operator op)
Parameters
System.Type type

Operator op

Returns
System.Reflection.MethodInfo[]

GetReturnType(MemberInfo)
FieldInfo will return the fieldType, propertyInfo the PropertyType, MethodInfo the return type and EventInfo will return the EventHandlerType.
public static Type GetReturnType(this MemberInfo memberInfo)
Parameters
System.Reflection.MemberInfo memberInfo

The MemberInfo.

Returns
System.Type

HasDefaultConstructor(Type)

Determines if a given System.Type has a default constructor.

A type is considered to have a default constructor if: It is a string, if it is an array, if it is a value type or if it has a public parameterless constructor.

public static bool HasDefaultConstructor(this Type self)
Parameters
System.Type self

The System.Type to investigate.

Returns
System.Boolean

true if a default constructor has been found; otherwise false.

HasNonDefaultContractTypes(ConstructorInfo)
public static bool HasNonDefaultContractTypes(ConstructorInfo info)
Parameters
System.Reflection.ConstructorInfo info

Returns
System.Boolean

HasParamaters(MethodInfo, IList<Type>, Boolean)
Determines whether a method has the specified parameter types.
public static bool HasParamaters(this MethodInfo methodInfo, IList<Type> paramTypes, bool inherit = true)
Parameters
System.Reflection.MethodInfo methodInfo

System.Collections.Generic.IList<System.Type> paramTypes

System.Boolean inherit

Returns
System.Boolean

ImplementsOpenGenericClass(Type, Type)
Determines whether a type implements an open generic class such as List<>.
public static bool ImplementsOpenGenericClass(this Type candidateType, Type openGenericType)
Parameters
System.Type candidateType

Type of the candidate.

System.Type openGenericType

Type of the open generic interface.

Returns
System.Boolean

ImplementsOpenGenericInterface(Type, Type)
Determines whether a type implements an open generic interface such as IList<>.
public static bool ImplementsOpenGenericInterface(this Type candidateType, Type openGenericInterfaceType)
Parameters
System.Type candidateType

Type of the candidate.

System.Type openGenericInterfaceType

Type of the open generic interface.

Returns
System.Boolean

Exceptions
System.ArgumentNullException

System.ArgumentException

Type " + openGenericInterfaceType.Name + " is not a generic type definition and an interface.

ImplementsOpenGenericType(Type, Type)
Determines whether a type implements an open generic interface or class such as IList<> or List<>.
public static bool ImplementsOpenGenericType(this Type candidateType, Type openGenericType)
Parameters
System.Type candidateType

Type of the candidate.

System.Type openGenericType

Type of the open generic type.

Returns
System.Boolean

ImplementsOrInherits(Type, Type)
Determines whether a type implements or inherits from another type.
public static bool ImplementsOrInherits(this Type type, Type to)
Parameters
System.Type type

The type.

System.Type to

To.

Returns
System.Boolean

InheritsFrom(Type, Type)
Determines whether a type inherits or implements another type. Also include support for open generic base types such as List<>.
public static bool InheritsFrom(this Type type, Type baseType)
Parameters
System.Type type

System.Type baseType

Returns
System.Boolean

InheritsFrom<TBase>(Type)
Determines whether a type inherits or implements another type. Also include support for open generic base types such as List<>.
public static bool InheritsFrom<TBase>(this Type type)
Parameters
System.Type type

Returns
System.Boolean

Type Parameters
TBase

InstantiateDefault(Type, Boolean)
Attempts to instantiate an object of a given System.Type with it's default constructor. If no default constructor is found then it attempts to find the most suitable alternative constructor and instantiate the System.Type with default parameters.
public static object InstantiateDefault(this Type type, bool preferUninitializedOverNonDefault)
Parameters
System.Type type

The System.Type to instantiate.

System.Boolean preferUninitializedOverNonDefault

Determines if the use of System.Runtime.Serialization.FormatterServices.GetUninitializedObject(System.Type) is preferred over a non-default constructor call.

Returns
System.Object

The instantiated object or null if no suitable alternative constructor is found.

IsCastableTo(Type, Type, Boolean)
Determines whether a type can be casted to another type.
public static bool IsCastableTo(this Type from, Type to, bool requireImplicitCast = false)
Parameters
System.Type from

From.

System.Type to

To.

System.Boolean requireImplicitCast

if set to true an implicit or explicit operator must be defined on the given type.

Returns
System.Boolean

IsCSharpKeyword(String)
Gets a value indicating if the string is a reserved C# keyword.
public static bool IsCSharpKeyword(string identifier)
Parameters
System.String identifier

The identifier to check.

Returns
System.Boolean

true if the string is a C# keyword. Otherwise false.

IsDefined<T>(Type)
Returns true if the attribute whose type is specified by the generic argument is defined on this type
public static bool IsDefined<T>(this Type type)
    where T : Attribute
Parameters
System.Type type

Returns
System.Boolean

Type Parameters
T

IsDefined<T>(Type, Boolean)
Returns true if the attribute whose type is specified by the generic argument is defined on this type
public static bool IsDefined<T>(this Type type, bool inherit)
    where T : Attribute
Parameters
System.Type type

System.Boolean inherit

Returns
System.Boolean

Type Parameters
T

IsFullyConstructedGenericType(Type)
Determines whether a type is a fully constructed generic type.
public static bool IsFullyConstructedGenericType(this Type type)
Parameters
System.Type type

Returns
System.Boolean

IsNullableType(Type)
Determines whether a type is nullable by ensuring the type is neither a PrimitiveType, ValueType or an Enum.
public static bool IsNullableType(this Type type)
Parameters
System.Type type

Returns
System.Boolean

IsUnmanagedCtor(ConstructorInfo)
Checks if a given System.Reflection.ConstructorInfo is unmanaged, by checking if it contains any System.Type.IsPointer, System.Type.IsByRef or System.Type.IsMarshalByRef parameters.
public static bool IsUnmanagedCtor(this ConstructorInfo ctor)
Parameters
System.Reflection.ConstructorInfo ctor

The System.Reflection.ConstructorInfo to validate.

Returns
System.Boolean

true if the System.Reflection.ConstructorInfo is unmanaged; otherwise false.

IsValidIdentifier(String)
Checks whether a given string is a valid CSharp identifier name. This also checks full type names including namespaces.
public static bool IsValidIdentifier(string identifier)
Parameters
System.String identifier

The identifier to check.

Returns
System.Boolean

SafeGetCustomAttributes(Assembly, Type, Boolean)
public static object[] SafeGetCustomAttributes(this Assembly assembly, Type type, bool inherit)
Parameters
System.Reflection.Assembly assembly

System.Type type

System.Boolean inherit

Returns
System.Object[]

SafeGetTypes(Assembly)
public static Type[] SafeGetTypes(this Assembly assembly)
Parameters
System.Reflection.Assembly assembly

Returns
System.Type[]

SafeIsDefined(Assembly, Type, Boolean)
public static bool SafeIsDefined(this Assembly assembly, Type attribute, bool inherit)
Parameters
System.Reflection.Assembly assembly

System.Type attribute

System.Boolean inherit

Returns
System.Boolean

SetMemberValue(MemberInfo, Object, Object)
Sets the value of a given MemberInfo. Currently only System.Reflection.FieldInfo and System.Reflection.PropertyInfo is supported.
public static void SetMemberValue(this MemberInfo member, object obj, object value)
Parameters
System.Reflection.MemberInfo member

The System.Reflection.MemberInfo to set the value of.

System.Object obj

The object to set the value on.

System.Object value

The value to set.

Exceptions
System.ArgumentException

Property has no setter or Can't set the value of the given System.Reflection.MemberInfo type.

TryInferGenericParameters(Type, out Type[], Type[])
// Tries to infer a set of valid generic parameters for a generic type definition, given a subset of known parameters.
public static bool TryInferGenericParameters(this Type genericTypeDefinition, out Type[] inferredParams, params Type[] knownParameters)
Parameters
System.Type genericTypeDefinition

The generic type definition to attempt to infer parameters for.

System.Type[] inferredParams

The inferred parameters, if inferral was successful.

System.Type[] knownParameters

The known parameters to infer from.

Returns
System.Boolean

True if the parameters could be inferred, otherwise, false.

Exceptions
System.ArgumentNullException

genericTypeDefinition is null or knownParameters is null

System.ArgumentException

The genericTypeDefinition parameter must be a generic type definition.