Class TypeFns
A collection of static methods used to provide additional Type related services.
public static class TypeFns
- Inheritance
-
TypeFns
- Inherited Members
Fields
DecimalTypes
Returns an array of decimal types.
public static readonly Type[] DecimalTypes
Field Value
- Type[]
IntegerTypes
Returns an array of integer types.
public static readonly Type[] IntegerTypes
Field Value
- Type[]
NumericTypes
Returns an array of numeric types.
public static readonly Type[] NumericTypes
Field Value
- Type[]
PredefinedTypes
Returns an array of predefined types.
public static readonly Type[] PredefinedTypes
Field Value
- Type[]
Methods
ConstructGenericInstance(Type, params Type[])
Constructs a generic instance.
public static object? ConstructGenericInstance(Type genericType, params Type[] argTypes)
Parameters
Returns
- object
The new instance; null only when the constructed type is a Nullable<T>.
ConstructGenericInstance(Type, Type[], params object?[])
Constructs a generic instance. Can only access public constructors.
public static object? ConstructGenericInstance(Type genericType, Type[] argTypes, params object?[] constructorParams)
Parameters
Returns
- object
The new instance; null only when the constructed type is a Nullable<T>.
FindGenericMethods(Type, string, BindingFlags, Type[]?)
Finds a collection of generic methods.
public static IEnumerable<MethodInfo> FindGenericMethods(Type type, string methodName, BindingFlags flags, Type[]? genericArgTypes)
Parameters
typeTypemethodNamestringIs case insensitive
flagsBindingFlagsgenericArgTypesType[]
Returns
FindIEnumerable(Type?)
public static Type? FindIEnumerable(Type? seqType)
Parameters
seqTypeType
Returns
FindMethod(Type, string, bool, Type[])
Finds a specific method.
public static MethodInfo? FindMethod(Type type, string methodName, bool isStatic, Type[] parameterTypes)
Parameters
Returns
FindMethod(Type, string, BindingFlags, Type[])
Finds a specific method.
public static MethodInfo? FindMethod(Type type, string methodName, BindingFlags flags, Type[] parameterTypes)
Parameters
typeTypemethodNamestringflagsBindingFlagsparameterTypesType[]
Returns
FindMethod(Type, string, BindingFlags, Type[]?, Type[])
Finds a specific method.
public static MethodInfo? FindMethod(Type type, string methodName, BindingFlags flags, Type[]? genericArgTypes, Type[] parameterTypes)
Parameters
typeTypemethodNamestringflagsBindingFlagsgenericArgTypesType[]parameterTypesType[]
Returns
FindMethod(Type, string, Type[])
Finds a specific public method.
public static MethodInfo? FindMethod(Type type, string methodName, Type[] parameterTypes)
Parameters
Returns
FindMethods(Type, string, BindingFlags, Type[])
Finds specific methods.
public static IEnumerable<MethodInfo> FindMethods(Type type, string methodName, BindingFlags flags, Type[] parameterTypes)
Parameters
typeTypemethodNamestringflagsBindingFlagsparameterTypesType[]
Returns
FindMethods(Type, string, BindingFlags, Type[]?, Type[])
Finds all the methods that match specific criteria.
public static IEnumerable<MethodInfo> FindMethods(Type type, string methodName, BindingFlags flags, Type[]? genericArgTypes, Type[] parameterTypes)
Parameters
typeTypemethodNamestringflagsBindingFlagsgenericArgTypesType[]parameterTypesType[]
Returns
FindPropertyOrField(Type, string, bool, bool)
Finds a specific public property or field. Will be automatically restricted as well by execution environment restrictions ( e.g. Silverlight).
public static MemberInfo? FindPropertyOrField(Type type, string memberName, bool isPublic, bool isStatic)
Parameters
Returns
FindPropertyOrField(Type, string, BindingFlags)
Finds a specific property or field
public static MemberInfo? FindPropertyOrField(Type type, string memberName, BindingFlags bindingFlags)
Parameters
typeTypememberNamestringbindingFlagsBindingFlags
Returns
GetDefaultValue(Type?)
Gets the default value for a specified type.
public static object? GetDefaultValue(Type? type)
Parameters
typeType
Returns
GetElementType(Type)
Returns the element type of any enumerable type;
public static Type? GetElementType(Type seqType)
Parameters
seqTypeType
Returns
GetGenericArgument(Type)
Gets a single generic argument from a specified type.
public static Type? GetGenericArgument(Type type)
Parameters
typeType
Returns
- Type
null if it can't find one or result is ambiguous
GetGroupingInterface(Type)
Returns the IGrouping interface implemented by the type.
public static Type? GetGroupingInterface(Type type)
Parameters
typeType
Returns
- Type
May return null if the specified type is not a grouping type
GetMethodByExample<TIn, TOut>(Expression<Func<TIn, TOut>>, params Type[])
For internal use only.
public static MethodInfo GetMethodByExample<TIn, TOut>(Expression<Func<TIn, TOut>> prototypeLambda, params Type[] resolvedTypes)
Parameters
prototypeLambdaExpression<Func<TIn, TOut>>resolvedTypesType[]
Returns
Type Parameters
TInTOut
GetMethodByExample<TIn1, TIn2, TOut>(Expression<Func<TIn1, TIn2, TOut>>, params Type[])
Find a two-argument method by writing an example call to it, then close it over different types.
public static MethodInfo GetMethodByExample<TIn1, TIn2, TOut>(Expression<Func<TIn1, TIn2, TOut>> prototypeLambda, params Type[] resolvedTypes)
Parameters
prototypeLambdaExpression<Func<TIn1, TIn2, TOut>>A lambda whose body is a call to the wanted method.
resolvedTypesType[]The type arguments to close the method over; none leaves it as found.
Returns
- MethodInfo
The method, closed over
resolvedTypesif it is generic.
Type Parameters
TIn1First parameter type of the example call.
TIn2Second parameter type of the example call.
TOutReturn type of the example call.
Remarks
Reflection over overloaded generic methods is awkward and easy to get wrong, so the method is named by a lambda that calls it - the compiler does the overload resolution - and the resulting definition is then re-closed over the types actually wanted.
GetMethodByNameAndType(Type, string, params Type[])
Find a method by name on a type, closing the type over the given type arguments first if it is generic.
public static MethodInfo? GetMethodByNameAndType(Type containingType, string methodName, params Type[] genericTypeArgs)
Parameters
containingTypeTypeThe type declaring the method, open if generic - e.g. typeof(List<>).
methodNamestringThe method name.
genericTypeArgsType[]Type arguments used to close
containingType.
Returns
- MethodInfo
The method, or null if the type has none by that name.
GetNonNullableType(Type)
Returns either the specified type or its non-nullable counterpart.
public static Type GetNonNullableType(Type type)
Parameters
typeType
Returns
GetNullableType(Type)
Gets the nullable type that corresponds to the given type.
public static Type? GetNullableType(Type type)
Parameters
typeType
Returns
- Type
null for a value type that is not one of the predefined numeric, date/time, Boolean, Char or Guid types (e.g. SByte, DateOnly, TimeOnly, enums, custom structs)
GetSelfAndBaseClasses(Type)
Returns a collection of classes (not interfaces) from which the given type directly inherits.
public static IEnumerable<Type> GetSelfAndBaseClasses(Type type)
Parameters
typeType
Returns
GetSelfAndBaseTypes(Type)
Returns a collection of types from which the given type directly inherits or implements.
public static IEnumerable<Type> GetSelfAndBaseTypes(Type type)
Parameters
typeType
Returns
GetTypeName(Type)
Returns the name of either the specified type or its non-nullable counterpart.
public static string GetTypeName(Type type)
Parameters
typeType
Returns
- string
the name of the given type
IsCompatibleWith(Type, Type)
Determines whether the source type is compatible with the given target type.
public static bool IsCompatibleWith(Type source, Type target)
Parameters
Returns
Remarks
This is a better implementation than IsAssignableFrom(Type).
IsEnumType(Type)
Returns whether the specified type (or its non-nullable counterpart) represents an enumeration.
public static bool IsEnumType(Type type)
Parameters
typeType
Returns
- bool
true if the specified type represents an enumeration; false otherwise
IsGroupingType(Type)
Returns true if the Type is an IGrouping.
public static bool IsGroupingType(Type type)
Parameters
typeType
Returns
IsIntegralType(Type)
Returns whether the specified type (or its non-nullable counterpart) represents an integer type.
public static bool IsIntegralType(Type type)
Parameters
typeType
Returns
IsNullableType(Type)
Returns whether the specified type is a nullable generic type, i.e. Nullable{T}.
public static bool IsNullableType(Type type)
Parameters
typeType
Returns
- bool
true if the specified type is a nullable generic type; false otherwise
IsNumericType(Type)
Returns whether the specified type (or its non-nullable counterpart) represents a numeric type.
public static bool IsNumericType(Type type)
Parameters
typeType
Returns
- bool
true if the specified type is numeric; false otherwise
IsPredefinedType(Type)
Returns whether the specified type is one of the PredefinedTypes.
public static bool IsPredefinedType(Type type)
Parameters
typeType
Returns
- bool
true if the specified type is a predefined type; false otherwise
IsPredefinedType(Type, bool)
Returns whether the specified type is one of the PredefinedTypes and optionally includes nullable versions of the same in the check.
public static bool IsPredefinedType(Type type, bool includeNullable)
Parameters
Returns
IsSignedIntegralType(Type)
Returns whether the specified type (or its non-nullable counterpart) represents a signed integer type.
public static bool IsSignedIntegralType(Type type)
Parameters
typeType
Returns
IsUnsignedIntegralType(Type)
Returns whether the specified type (or its non-nullable counterpart) represents an unsigned integer type.
public static bool IsUnsignedIntegralType(Type type)
Parameters
typeType
Returns
MakeGenericList(Type)
Constructs a generic list.
public static IList MakeGenericList(Type type)
Parameters
typeType