TypeHelper Class
Namespace: PTS
Assembly: PTSframework.dll
Provides a set of static methods for Type
type.
Syntax
public static class TypeHelper
Inheritance
Methods
GetDefaultValue(Type)
Gets defualt value for given type.
Declaration
public static object GetDefaultValue(this Type classType)
Parameters
Type | Name | Description |
---|---|---|
System.Type | classType | Type to get the dafault value for. |
Returns
Type | Description |
---|---|
System.Object | Default value for given type. |
GetFieldOrProperty(Type, String)
Gets information object for field or property matching the specified name.
Declaration
public static MemberInfo GetFieldOrProperty(this Type type, string memberName)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type with the field or property. |
System.String | memberName | Field or property to look up. |
Returns
Type | Description |
---|---|
System.Reflection.MemberInfo |
|
GetFirstGenericBaseType(Type)
Gets the first generic type in base type tree.
Declaration
public static Type GetFirstGenericBaseType(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to get the first generic type in parent tree for. |
Returns
Type | Description |
---|---|
System.Type | The first generic type in base type tree |
GetGenericArgumentType(Type, Int32, Boolean)
Gets generic type argument at specified position.
Declaration
public static Type GetGenericArgumentType(this Type type, int position = 0, bool notGenericError = true)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to get the generic type argument for. |
System.Int32 | position | Position of the generic type argument. |
System.Boolean | notGenericError | If set to |
Returns
Type | Description |
---|---|
System.Type | generic type argument at specified position. |
Exceptions
Type | Condition |
---|---|
System.InvalidCastException | Thrown if the type does not have generic type arguments. |
System.ArgumentOutOfRangeException | Thrown if the position is out of range of type arguments present in given type. |
System.InvalidCastException | Thrown if the specified type is not generic and the flag parameter is set to |
GetGenericPrettyName(Type, Boolean)
Gets generic type name with generic type arguments as if written in csharp code. Nested generic type argument names will be formatted recursively.
Declaration
public static string GetGenericPrettyName(this Type type, bool withNamespace = false)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to get the formatted generic name for. |
System.Boolean | withNamespace | If set to |
Returns
Type | Description |
---|---|
System.String | Generic type name with generic type arguments as if written in csharp code. |
GetImplementedInterfaces(Type)
Gets collection of interface types directly implemented by given type. Inherited interface types are excluded.
Declaration
public static Type[] GetImplementedInterfaces(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to get the implemented interfaces for. |
Returns
Type | Description |
---|---|
System.Type[] | Interface types directly implemented by given type. |
GetNullableUnderlyingType(Type)
Gets underlying primitive type of nullable value type.
Declaration
public static Type GetNullableUnderlyingType(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to get the underlying type for. |
Returns
Type | Description |
---|---|
System.Type | Underlying primitive type. |
GetTypeTree(Type, Type)
Gets collection of given type and base types in descending order.
Declaration
public static HashSet<Type> GetTypeTree(this Type type, Type stopAt = null)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to get the type tree for. |
System.Type | stopAt | Terminal base type that cuts off the traversal. |
Returns
Type | Description |
---|---|
System.Collections.Generic.HashSet<System.Type> | Collection of given type and base types in descending order. |
HasCustomAttribute(Type, Type)
Checks whether given type has specific custom attribute.
Declaration
public static bool HasCustomAttribute(this Type type, Type attributeType)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to peform the custom attribute look up for. |
System.Type | attributeType | Type of custom attribute to look for. |
Returns
Type | Description |
---|---|
System.Boolean |
|
HasCustomAttribute<TAttribute>(Type)
Checks whether given type has specific custom attribute.
Declaration
public static bool HasCustomAttribute<TAttribute>(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to peform the custom attribute look up for. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Type Parameters
Name | Description |
---|---|
TAttribute | Type of custom attribute to look for. |
HasGenericTypeDefinition(Type, Type)
Checks whether given type is inherited from specified generic type definition.
Declaration
public static bool HasGenericTypeDefinition(this Type type, Type genericTypeDefinition)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to determine the result for. |
System.Type | genericTypeDefinition | Generic type definition to look for. |
Returns
Type | Description |
---|---|
System.Boolean | If given type is derived from specified generic type definition then |
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | Thrown if second parameter is not generic type definition. |
ImplementsInterface(Type, Type)
Checks whether given type implements interface type.
Declaration
public static bool ImplementsInterface(this Type type, Type interfaceType)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to perform the test test. |
System.Type | interfaceType | Interface type to look for. |
Returns
Type | Description |
---|---|
System.Boolean | If type implements the interface then |
ImplementsInterfaceDirectly(Type, Type)
Checks whether given type implements directly interface type.
Declaration
public static bool ImplementsInterfaceDirectly(this Type type, Type interfaceType)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type that potentially implements the interface. |
System.Type | interfaceType | Interface type to check the implementation of. |
Returns
Type | Description |
---|---|
System.Boolean | If the interface is implemented directly by type then |
Remarks
The method looks only for interfaces implemented directly by specified type, inherited interfaces are ignored.
ImplementsInterfaces(Type, Type[])
Checks whether given type implements range of interface types.
Declaration
public static bool ImplementsInterfaces(this Type type, params Type[] interfaceTypes)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to perform the test on. |
System.Type[] | interfaceTypes | Interface types to look for. |
Returns
Type | Description |
---|---|
System.Boolean | If type implements all interfaces in collection then |
ImplementsInterfacesDirectly(Type, Type[])
Checks whether given type implements directly range of interface types.
Declaration
public static bool ImplementsInterfacesDirectly(this Type type, params Type[] interfaceTypes)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type that potentially implements the interfaces. |
System.Type[] | interfaceTypes | Interface type to check the implementation of. |
Returns
Type | Description |
---|---|
System.Boolean | If all interfaces are implemented directly by the type then |
Remarks
The method looks only for interfaces implemented directly by specified type, inherited interfaces are ignored.
IsAssignableToGenericType(Type, Type)
Declaration
public static bool IsAssignableToGenericType(this Type givenType, Type genericType)
Parameters
Type | Name | Description |
---|---|---|
System.Type | givenType | |
System.Type | genericType |
Returns
Type | Description |
---|---|
System.Boolean |
IsBaseTypeOf(Type, Type)
Check whether given type is parent to another type.
Declaration
public static bool IsBaseTypeOf(this Type type, Type inheritedType)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Potential parent type of the other type. |
System.Type | inheritedType | Type that potentially inherits from given type. |
Returns
Type | Description |
---|---|
System.Boolean | If the given type is parent to the other type then |
Remarks
This method has inverse parameters of the native Type.IsSubclassOf
method.
IsImplementedDirectlyOn(Type, Type)
Checks whether given inteface type is implemented directly on a type.
Declaration
public static bool IsImplementedDirectlyOn(this Type interfaceType, Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | interfaceType | Interface type to check the implementation of. |
System.Type | type | The type that potentially implements the interface. |
Returns
Type | Description |
---|---|
System.Boolean | If the interface is implemented directly by type then |
Remarks
The method looks only for interfaces implemented directly by specified type, inherited interfaces are ignored.
IsIntegral(Type)
Checks wether given type is of integer type value.
Declaration
public static bool IsIntegral(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to perform the test for. |
Returns
Type | Description |
---|---|
System.Boolean | If the type is integer type then |
IsNullable(Type)
Checks whether given type is nullable primitive type.
Declaration
public static bool IsNullable(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to perform the test for. |
Returns
Type | Description |
---|---|
System.Boolean | If given type is nullable primitive type then |
IsNullableEnum(Type)
Checks whether given type is nullable enum type.
Declaration
public static bool IsNullableEnum(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to perform the test for. |
Returns
Type | Description |
---|---|
System.Boolean | If given type is nullable |
IsNumeric(Type)
Checks wether given type is of primitive numeric value type.
Declaration
public static bool IsNumeric(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to perform the test for. |
Returns
Type | Description |
---|---|
System.Boolean | If the type is numeric value type then |
IsPrimitiveNullable(Type)
Checks wether given type is of nullable primitive numeric value type.
Declaration
public static bool IsPrimitiveNullable(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to perform the test for. |
Returns
Type | Description |
---|---|
System.Boolean | If the type is numeric numeric value type then |
IsSameOrDerivedFrom(Type, Type)
Checks whether given type is either the same or derived from another type.
Declaration
public static bool IsSameOrDerivedFrom(this Type type, Type baseType)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to peform the test for. |
System.Type | baseType | Other type that is either the same or base type of given type. |
Returns
Type | Description |
---|---|
System.Boolean | If given type is either the same or derived from other type then |
IsSameOrDerivedFrom<TParent>(Type)
Checks whether given type is either the same or derived from another type.
Declaration
public static bool IsSameOrDerivedFrom<TParent>(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to peform the test for. |
Returns
Type | Description |
---|---|
System.Boolean |
Type Parameters
Name | Description |
---|---|
TParent |
IsSimple(Type)
Checks whether given type is either primitive value type, enum or string in both nullable or direct variant.
Declaration
public static bool IsSimple(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to perform the test for. |
Returns
Type | Description |
---|---|
System.Boolean | If given type is primitive value type, nullable value type, or string then |
IsText(Type)
Checks whether given type is either string, char or char[] type.
Declaration
public static bool IsText(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to perform the test for. |
Returns
Type | Description |
---|---|
System.Boolean | If given type is string, char or char[] the |
IsValueDefault(Type, Object)
Checks whether specified object represents default value for given type.
Declaration
public static bool IsValueDefault(this Type type, object value)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to perform the test for. |
System.Object | value | Value to check whether is default or not. |
Returns
Type | Description |
---|---|
System.Boolean | If specified object is of default value for give type then |
ToConcreteTypeFromConstraints(Type)
Converts given generic type definition to concrete type with constraint types as generic type parameters.
Declaration
public static Type ToConcreteTypeFromConstraints(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Generic type definition to get concreted version for. |
Returns
Type | Description |
---|---|
System.Type | Concrete type with constraint types as genereric type parameters. |
ToNullableType(Type)
Converts primitive value type to its nullable equivalent.
Declaration
public static Type ToNullableType(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Value type to convert to nullable. |
Returns
Type | Description |
---|---|
System.Type | Nullable value type. |
Exceptions
Type | Condition |
---|---|
System.InvalidCastException | Thrown if the type is not value type. |