Class TypeUtils
Assembly: Xareus.Utils.dll
Syntax
public static class TypeUtils
Methods
CleanTypeName(string)
If provided with the name of a generic Type, removes the `X generic indicator at the end
Declaration
public static string CleanTypeName(string typeName)
Parameters
Type |
Name |
Description |
string |
typeName |
Type name to be "cleaned"
|
Returns
Type |
Description |
string |
Name without the generic indicator, if any
|
GetClosestType(Type, List<Type>)
Get the closest type matching the first type inheritance tree from a list of types.
Declaration
public static Type GetClosestType(this Type type, List<Type> listTypes)
Parameters
Returns
Examples
class A:B {}
class B:C {}
GetClosestType(A, [B, C, D]); // => B
GetClosestType(A, [C, D]); // => C
GetClosestType(A, [D]); // => null
GetConstructorWithCache(Type, Type[])
Gets the (public, protected or private) constructor of the given type with the given parameters, from cache if possible
Declaration
public static ConstructorInfo GetConstructorWithCache(this Type type, Type[] parametersTypes)
Parameters
Type |
Name |
Description |
Type |
type |
|
Type[] |
parametersTypes |
|
Returns
GetFieldsAndProperties(Type)
Search using Reflection for FieldInfo and PropertyInfo on the given Type.
Declaration
public static FieldsAndPropertiesContainer GetFieldsAndProperties(this Type type)
Parameters
Type |
Name |
Description |
Type |
type |
|
Returns
GetFieldsAndPropertiesOfType(Type, Type)
Search using Reflection for FieldInfo and PropertyInfo of the given type on the given Type.
Declaration
public static FieldsAndPropertiesContainer GetFieldsAndPropertiesOfType(this Type type, Type memberType)
Parameters
Type |
Name |
Description |
Type |
type |
|
Type |
memberType |
|
Returns
GetFieldsAndPropertiesWithAttribute(Type, Type)
Search using Reflection for FieldInfo and PropertyInfo with the given attribute type on the given Type.
Declaration
public static FieldsAndPropertiesContainer GetFieldsAndPropertiesWithAttribute(this Type type, Type attributeType)
Parameters
Type |
Name |
Description |
Type |
type |
|
Type |
attributeType |
|
Returns
GetFieldsWithAttribute<T>(Type, bool)
Get all the fields having an attribute
Declaration
public static IEnumerable<FieldInfo> GetFieldsWithAttribute<T>(this Type type, bool includeNonPublic = true) where T : Attribute
Parameters
Type |
Name |
Description |
Type |
type |
|
bool |
includeNonPublic |
include non public fields in the results
|
Returns
Type |
Description |
IEnumerable<FieldInfo> |
A IEnumerable containing all types that have the attribute T T
|
Type Parameters
Name |
Description |
T |
Attribute type the fields must have
|
GetHigherType(Type, Type)
Returns the hierachicaly higher type
Declaration
public static Type GetHigherType(Type type1, Type type2)
Parameters
Type |
Name |
Description |
Type |
type1 |
|
Type |
type2 |
|
Returns
GetInterfacesWithCache(Type)
Declaration
public static Type[] GetInterfacesWithCache(this Type type)
Parameters
Type |
Name |
Description |
Type |
type |
|
Returns
GetMethodsFiltered(Type)
Get all the public and static methods of the given type, excluding properties setters and getters
Declaration
public static IEnumerable<MethodInfo> GetMethodsFiltered(this Type type)
Parameters
Type |
Name |
Description |
Type |
type |
|
Returns
Exceptions
GetName(Type)
Declaration
public static string GetName(this Type type)
Parameters
Type |
Name |
Description |
Type |
type |
|
Returns
GetPropertiesWithAttribute<T>(Type, bool)
Get all the properties having an attribute
Declaration
public static IEnumerable<PropertyInfo> GetPropertiesWithAttribute<T>(this Type type, bool includeNonPublic = true) where T : Attribute
Parameters
Type |
Name |
Description |
Type |
type |
|
bool |
includeNonPublic |
include non public properties in the results
|
Returns
Type Parameters
Name |
Description |
T |
Attribute type the fields must have
|
GetTypeDistance(Type, Type)
Get the distance between the first type and the second type in the inheritance tree.
If the second type is not in the inheritance tree of the first type, returns -1.
Declaration
public static int GetTypeDistance(this Type type, Type otherType)
Parameters
Type |
Name |
Description |
Type |
type |
|
Type |
otherType |
|
Returns
Examples
if A derives directly from B :
class A : B {}
GetTypeDistance(A, B) => 1
GetTypeDistance(B, A) => -1
GetTypesImplementingInterface<T>(bool)
List all types in the Assemblies that implement a given interface
Declaration
public static IEnumerable<Type> GetTypesImplementingInterface<T>(bool ignoreCache = false)
Parameters
Type |
Name |
Description |
bool |
ignoreCache |
|
Returns
Type |
Description |
IEnumerable<Type> |
A IEnumerable containing all types that inherit from T T
|
Type Parameters
Name |
Description |
T |
Class the listed types inherit from
|
GetTypesInheritingFrom(Type, bool, bool)
List all types in the Assemblies that inherit from a given class.
Result is not stored within static types. This could lead to inconsistencies with previous code.
(IE: The user may expect to find abstract types within "inheritingTypes").
Declaration
public static IEnumerable<Type> GetTypesInheritingFrom(this Type baseType, bool includeAbstract = true, bool ignoreCache = false)
Parameters
Type |
Name |
Description |
Type |
baseType |
|
bool |
includeAbstract |
|
bool |
ignoreCache |
|
Returns
GetTypesInheritingFrom<T>(bool, bool)
List all types in the Assemblies that inherit from a given class.
Result is not stored within static types. This could lead to inconsistencies with previous code.
(IE: The user may expect to find abstract types within "inheritingTypes").
Declaration
public static IEnumerable<Type> GetTypesInheritingFrom<T>(bool includeAbstract = true, bool ignoreCache = false) where T : class
Parameters
Type |
Name |
Description |
bool |
includeAbstract |
|
bool |
ignoreCache |
|
Returns
Type Parameters
GetTypesWithAttribute<T>(bool)
List all types in the Assemblies that holds the given attribute
Declaration
public static IEnumerable<Type> GetTypesWithAttribute<T>(bool ignoreCache = false) where T : Attribute
Parameters
Type |
Name |
Description |
bool |
ignoreCache |
|
Returns
Type |
Description |
IEnumerable<Type> |
A IEnumerable containing all types that have the attribute T T
|
Type Parameters
Name |
Description |
T |
Attribute the listed types have from
|
IsAssignableToGenericType(Type, Type)
Check if the given type is assignable from the current generic type
Declaration
public static bool IsAssignableToGenericType(this Type genericType, Type givenType)
Parameters
Type |
Name |
Description |
Type |
genericType |
|
Type |
givenType |
|
Returns