Namespace: | Sirenix.Utilities |
Assembly: | Sirenix.Utilities |
public static class TypeExtensions
public static readonly Dictionary<Type, string> TypeNameAlternatives
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)
System.Reflection.MethodBase | genericMethod | The generic method definition to check. |
System.Type[] | parameters | The parameters to check validity for. |
System.Boolean |
System.ArgumentNullException | genericType is null or types is null |
System.ArgumentException | The genericMethod parameter must be a generic method definition. |
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)
System.Type | genericType | The generic type definition to check. |
System.Type[] | parameters | The parameters to check validity for. |
System.Boolean |
System.ArgumentNullException | genericType is null or types is null |
System.ArgumentException | The genericType parameter must be a generic type definition. |
public static bool AreGenericConstraintsSatisfiedBy(Type[] definitions, Type[] parameters)
System.Type[] | definitions | |
System.Type[] | parameters |
System.Boolean |
public static ConstructorInfo FindIdealConstructor(Type type, BindingFlags flags = BindingFlags.Default)
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. |
System.Reflection.ConstructorInfo | The most ideal System.Reflection.ConstructorInfo based on the scoring system. |
public static bool GenericArgumentsContainsTypes(this Type type, params Type[] types)
System.Type | type | The type. |
System.Type[] | types | The generic argument types. |
System.Boolean |
public static bool GenericParameterIsFulfilledBy(this Type genericParameterDefinition, Type parameterType)
System.Type | genericParameterDefinition | |
System.Type | parameterType |
System.Boolean |
public static IEnumerable<MemberInfo> GetAllMembers(this Type type, BindingFlags flags = BindingFlags.Default)
System.Type | type | |
System.Reflection.BindingFlags | flags |
System.Collections.Generic.IEnumerable<System.Reflection.MemberInfo> |
public static IEnumerable<MemberInfo> GetAllMembers(this Type type, string name, BindingFlags flags = BindingFlags.Default)
System.Type | type | |
System.String | name | |
System.Reflection.BindingFlags | flags |
System.Collections.Generic.IEnumerable<System.Reflection.MemberInfo> |
public static IEnumerable<T> GetAllMembers<T>(this Type type, BindingFlags flags = BindingFlags.Default)
where T : MemberInfo
System.Type | type | |
System.Reflection.BindingFlags | flags |
System.Collections.Generic.IEnumerable<T> |
T |
public static Type[] GetArgumentsOfInheritedOpenGenericClass(this Type candidateType, Type openGenericType)
System.Type | candidateType | Type of the candidate. |
System.Type | openGenericType | Type of the open generic class. |
System.Type[] |
public static Type[] GetArgumentsOfInheritedOpenGenericInterface(this Type candidateType, Type openGenericInterfaceType)
System.Type | candidateType | Type of the candidate. |
System.Type | openGenericInterfaceType | Type of the open generic interface. |
System.Type[] |
public static Type[] GetArgumentsOfInheritedOpenGenericType(this Type candidateType, Type openGenericType)
System.Type | candidateType | Type of the candidate. |
System.Type | openGenericType | The open generic type to get the arguments of. |
System.Type[] |
public static T GetAttribute<T>(this Type type, bool inherit)
where T : Attribute
System.Type | type | The type. |
System.Boolean | inherit | If true, specifies to also search the ancestors of element for custom attributes. |
T |
T |
public static IEnumerable<Type> GetBaseClasses(this Type type, bool includeSelf = false)
System.Type | type | |
System.Boolean | includeSelf |
System.Collections.Generic.IEnumerable<System.Type> |
public static IEnumerable<Type> GetBaseTypes(this Type type, bool includeSelf = false)
System.Type | type | |
System.Boolean | includeSelf |
System.Collections.Generic.IEnumerable<System.Type> |
public static MethodInfo GetCastMethod(this Type from, Type to, bool requireImplicitCast = false)
System.Type | from | From. |
System.Type | to | To. |
System.Boolean | requireImplicitCast | if set to |
System.Reflection.MethodInfo |
public static Func<object, object> GetCastMethodDelegate(this Type from, Type to, bool requireImplicitCast = false)
System.Type | from | From. |
System.Type | to | To. |
System.Boolean | requireImplicitCast | if set to |
System.Func<System.Object, System.Object> |
public static Func<TFrom, TTo> GetCastMethodDelegate<TFrom, TTo>(bool requireImplicitCast = false)
System.Boolean | requireImplicitCast | if set to |
System.Func<TFrom, TTo> |
TFrom | |
TTo |
public static string GetCompilableNiceFullName(this Type type)
System.Type | type | The type. |
System.String |
public static string GetCompilableNiceName(this Type type)
System.Type | type | The type. |
System.String |
public static int GetCtorScore(ConstructorInfo ctor)
System.Reflection.ConstructorInfo | ctor |
System.Int32 |
public static T GetCustomAttribute<T>(this Type type)
where T : Attribute
System.Type | type |
T |
T |
public static T GetCustomAttribute<T>(this Type type, bool inherit)
where T : Attribute
System.Type | type | |
System.Boolean | inherit |
T |
T |
public static IEnumerable<T> GetCustomAttributes<T>(this Type type)
where T : Attribute
System.Type | type | The type. |
System.Collections.Generic.IEnumerable<T> |
T |
public static IEnumerable<T> GetCustomAttributes<T>(this Type type, bool inherit)
where T : Attribute
System.Type | type | The type |
System.Boolean | inherit | If true, specifies to also search the ancestors of element for custom attributes. |
System.Collections.Generic.IEnumerable<T> |
T |
public static ulong GetEnumBitmask(object value, Type enumType)
System.Object | value | |
System.Type | enumType |
System.UInt64 |
System.ArgumentException | enumType |
public static Func<T, T, bool> GetEqualityComparerDelegate<T>()
System.Func<T, T, System.Boolean> |
T |
public static Type GetGenericBaseType(this Type type, Type baseType)
System.Type | type | |
System.Type | baseType |
System.Type |
public static Type GetGenericBaseType(this Type type, Type baseType, out int depthCount)
System.Type | type | |
System.Type | baseType | |
System.Int32 | depthCount |
System.Type |
public static string GetGenericConstraintsString(this Type type, bool useFullTypeNames = false)
System.Type | type | |
System.Boolean | useFullTypeNames |
System.String |
where T : class
public static string GetGenericParameterConstraintsString(this Type type, bool useFullTypeNames = false)
System.Type | type | |
System.Boolean | useFullTypeNames |
System.String |
public static int GetInheritanceDistance(this Type type, Type baseType)
System.Type | type | |
System.Type | baseType |
System.Int32 |
public static object GetMemberValue(this MemberInfo member, object obj)
System.Reflection.MemberInfo | member | The System.Reflection.MemberInfo to get the value of. |
System.Object | obj | The instance to get the value from. |
System.Object | The value contained in the given System.Reflection.MemberInfo. |
System.ArgumentException | Can't get the value of the given System.Reflection.MemberInfo type. |
public static string GetNiceFullName(this Type type)
System.Type | type |
System.String |
public static string GetNiceName(this Type type)
System.Type | type |
System.String |
public static MethodInfo GetOperatorMethod(this Type type, Operator op)
System.Type | type | |
Operator | op |
System.Reflection.MethodInfo |
public static MethodInfo GetOperatorMethod(this Type type, Operator op, Type leftOperand, Type rightOperand)
System.Type | type | |
Operator | op | |
System.Type | leftOperand | |
System.Type | rightOperand |
System.Reflection.MethodInfo |
public static MethodInfo[] GetOperatorMethods(this Type type, Operator op)
System.Type | type | |
Operator | op |
System.Reflection.MethodInfo[] |
public static Type GetReturnType(this MemberInfo memberInfo)
System.Reflection.MemberInfo | memberInfo | The MemberInfo. |
System.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)
System.Type | self | The System.Type to investigate. |
System.Boolean |
|
public static bool HasNonDefaultContractTypes(ConstructorInfo info)
System.Reflection.ConstructorInfo | info |
System.Boolean |
public static bool HasParamaters(this MethodInfo methodInfo, IList<Type> paramTypes, bool inherit = true)
System.Reflection.MethodInfo | methodInfo | |
System.Collections.Generic.IList<System.Type> | paramTypes | |
System.Boolean | inherit |
System.Boolean |
public static bool ImplementsOpenGenericClass(this Type candidateType, Type openGenericType)
System.Type | candidateType | Type of the candidate. |
System.Type | openGenericType | Type of the open generic interface. |
System.Boolean |
public static bool ImplementsOpenGenericInterface(this Type candidateType, Type openGenericInterfaceType)
System.Type | candidateType | Type of the candidate. |
System.Type | openGenericInterfaceType | Type of the open generic interface. |
System.Boolean |
System.ArgumentNullException | |
System.ArgumentException | Type " + openGenericInterfaceType.Name + " is not a generic type definition and an interface. |
public static bool ImplementsOpenGenericType(this Type candidateType, Type openGenericType)
System.Type | candidateType | Type of the candidate. |
System.Type | openGenericType | Type of the open generic type. |
System.Boolean |
public static bool ImplementsOrInherits(this Type type, Type to)
System.Type | type | The type. |
System.Type | to | To. |
System.Boolean |
public static bool InheritsFrom(this Type type, Type baseType)
System.Type | type | |
System.Type | baseType |
System.Boolean |
public static bool InheritsFrom<TBase>(this Type type)
System.Type | type |
System.Boolean |
TBase |
public static object InstantiateDefault(this Type type, bool preferUninitializedOverNonDefault)
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. |
System.Object | The instantiated object or |
public static bool IsCastableTo(this Type from, Type to, bool requireImplicitCast = false)
System.Type | from | From. |
System.Type | to | To. |
System.Boolean | requireImplicitCast | if set to |
System.Boolean |
public static bool IsCSharpKeyword(string identifier)
System.String | identifier | The identifier to check. |
System.Boolean |
|
public static bool IsDefined<T>(this Type type)
where T : Attribute
System.Type | type |
System.Boolean |
T |
public static bool IsDefined<T>(this Type type, bool inherit)
where T : Attribute
System.Type | type | |
System.Boolean | inherit |
System.Boolean |
T |
public static bool IsFullyConstructedGenericType(this Type type)
System.Type | type |
System.Boolean |
public static bool IsNullableType(this Type type)
System.Type | type |
System.Boolean |
public static bool IsUnmanagedCtor(this ConstructorInfo ctor)
System.Reflection.ConstructorInfo | ctor | The System.Reflection.ConstructorInfo to validate. |
System.Boolean |
|
public static bool IsValidIdentifier(string identifier)
System.String | identifier | The identifier to check. |
System.Boolean |
public static object[] SafeGetCustomAttributes(this Assembly assembly, Type type, bool inherit)
System.Reflection.Assembly | assembly | |
System.Type | type | |
System.Boolean | inherit |
System.Object[] |
public static Type[] SafeGetTypes(this Assembly assembly)
System.Reflection.Assembly | assembly |
System.Type[] |
public static bool SafeIsDefined(this Assembly assembly, Type attribute, bool inherit)
System.Reflection.Assembly | assembly | |
System.Type | attribute | |
System.Boolean | inherit |
System.Boolean |
public static void SetMemberValue(this MemberInfo member, object obj, object value)
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. |
System.ArgumentException | Property has no setter or Can't set the value of the given System.Reflection.MemberInfo type. |
public static bool TryInferGenericParameters(this Type genericTypeDefinition, out Type[] inferredParams, params Type[] knownParameters)
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. |
System.Boolean | True if the parameters could be inferred, otherwise, false. |
System.ArgumentNullException | genericTypeDefinition is null or knownParameters is null |
System.ArgumentException | The genericTypeDefinition parameter must be a generic type definition. |