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[], BindingFlags)
Gets the constructor of the given type with the given parameters, from cache if possible
Declaration
public static ConstructorInfo GetConstructorWithCache(this Type type, Type[] parametersTypes, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
Parameters
Returns
GetFieldsAndProperties(Type, BindingFlags)
Search using Reflection for FieldInfo and PropertyInfo on the given Type.
Declaration
public static FieldsAndPropertiesContainer GetFieldsAndProperties(this Type type, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
Parameters
Returns
GetFieldsAndPropertiesExcludingType(Type, Type, BindingFlags)
Get all the fields and properties of a Type, excluding the ones inherited from the given type to exclude.
Declaration
public static FieldsAndPropertiesContainer GetFieldsAndPropertiesExcludingType(this Type type, Type typeToExclude, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
Parameters
Type |
Name |
Description |
Type |
type |
Type you want to retrieve the members of
|
Type |
typeToExclude |
The type you want to exclude the members of
|
BindingFlags |
bindingFlags |
|
Returns
GetFieldsAndPropertiesOfType(Type, Type, BindingFlags)
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, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
Parameters
Returns
GetFieldsAndPropertiesWithAttribute(Type, Type, BindingFlags)
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, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
Parameters
Returns
GetFieldsWithAttribute<T>(Type, BindingFlags)
Get all the fields having an attribute
Declaration
public static IEnumerable<FieldInfo> GetFieldsWithAttribute<T>(this Type type, BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy) where T : Attribute
Parameters
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
|
GetFieldsWithCache(Type, BindingFlags)
Get all the fields of the given type, from cache if possible.
Declaration
public static IEnumerable<FieldInfo> GetFieldsWithCache(this Type type, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
Parameters
Returns
Exceptions
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
GetMethodsExcludingType(Type, Type, BindingFlags)
Get all the methods of a Type, excluding the ones inherited from the given type to exclude.
Declaration
public static IEnumerable<MethodInfo> GetMethodsExcludingType(this Type type, Type typeToExclude, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
Parameters
Type |
Name |
Description |
Type |
type |
Type you want to retrieve the members of
|
Type |
typeToExclude |
The type you want to exclude the members of
|
BindingFlags |
bindingFlags |
|
Returns
Type |
Description |
IEnumerable<MethodInfo> |
All the members of a Type, excluding the ones inherited from the given type to exclude.
|
GetMethodsWithCache(Type, BindingFlags)
Get all the methods of the given type, from cache if possible.
Declaration
public static IEnumerable<MethodInfo> GetMethodsWithCache(this Type type, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
Parameters
Returns
Exceptions
GetMethodsWithoutPropertyAccessors(Type, BindingFlags)
Get all the methods of the given type, excluding properties setters and getters
Declaration
public static IEnumerable<MethodInfo> GetMethodsWithoutPropertyAccessors(this Type type, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
Parameters
Returns
Exceptions
GetMethodsWithoutPropertyAccessorsExcludingType(Type, Type, BindingFlags)
Get all the methods of a Type excluding the ones from properties, excluding the ones inherited from the given type to exclude.
Declaration
public static IEnumerable<MethodInfo> GetMethodsWithoutPropertyAccessorsExcludingType(this Type type, Type typeToExclude, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
Parameters
Type |
Name |
Description |
Type |
type |
Type you want to retrieve the members of
|
Type |
typeToExclude |
The type you want to exclude the members of
|
BindingFlags |
bindingFlags |
|
Returns
Type |
Description |
IEnumerable<MethodInfo> |
All the members of a Type, excluding the ones inherited from the given type to exclude.
|
GetName(Type)
Declaration
public static string GetName(this Type type)
Parameters
Type |
Name |
Description |
Type |
type |
|
Returns
GetPropertiesWithAttribute<T>(Type, BindingFlags)
Get all the properties having an attribute
Declaration
public static IEnumerable<PropertyInfo> GetPropertiesWithAttribute<T>(this Type type, BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy) where T : Attribute
Parameters
Returns
Type Parameters
Name |
Description |
T |
Attribute type the fields must have
|
GetPropertiesWithCache(Type, BindingFlags)
Get all the properties of the given type, from cache if possible.
Declaration
public static IEnumerable<PropertyInfo> GetPropertiesWithCache(this Type type, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
Parameters
Returns
Exceptions
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