Class TypeExtensions
Extension methods for Type objects
Inherited Members
Namespace: Unity.XR.CoreUtils
Syntax
public static class TypeExtensions
Methods
GetAssignableTypes(Type, List<Type>, Func<Type, Boolean>)
Add all types assignable to this one to a list, using an optional predicate test
Declaration
public static void GetAssignableTypes(this Type type, List<Type> list, Func<Type, bool> predicate = null)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type to which assignable types will be matched |
List<Type> | list | The list to which assignable types will be appended |
Func<Type, Boolean> | predicate | Custom delegate to allow user filtering of type list. Return false to ignore given type |
GetAttribute<TAttribute>(Type, Boolean)
Gets the first attribute of a given type.
Declaration
public static TAttribute GetAttribute<TAttribute>(this Type type, bool inherit = false)
where TAttribute : Attribute
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type whose attribute will be returned |
Boolean | inherit | Whether to search this type's inheritance chain to find the attribute |
Returns
Type | Description |
---|---|
TAttribute | The first |
Type Parameters
Name | Description |
---|---|
TAttribute | Attribute type to return |
GetExtensionsOfClass(Type, List<Type>)
Find all types that extend the given class type, and append them to a list If the input type is not an class type, no action is taken.
Declaration
public static void GetExtensionsOfClass(this Type type, List<Type> list)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The class type of whom list will be found |
List<Type> | list | The list to which extension types will be appended |
GetFieldInTypeOrBaseType(Type, String)
Search by name through a fields of a type and its base types and return the field if one is found
Declaration
public static FieldInfo GetFieldInTypeOrBaseType(this Type type, string fieldName)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type to search |
String | fieldName | The name of the field to search for |
Returns
Type | Description |
---|---|
FieldInfo | The field, if found |
GetFieldRecursively(Type, String, BindingFlags)
Gets a specific field of the Type or any of its base Types
Declaration
public static FieldInfo GetFieldRecursively(this Type type, string name, BindingFlags bindingAttr)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type which will be searched for fields |
String | name | Name of the field to get |
BindingFlags | bindingAttr | A bitmask specifying how the search is conducted |
Returns
Type | Description |
---|---|
FieldInfo | An object representing the field that matches the specified requirements, if found; otherwise, null |
GetFieldsRecursively(Type, List<FieldInfo>, BindingFlags)
Gets all fields of the Type or any of its base Types
Declaration
public static void GetFieldsRecursively(this Type type, List<FieldInfo> fields, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
Type | type | Type we are going to get fields on |
List<FieldInfo> | fields | A list to which all fields of this type will be added |
BindingFlags | bindingAttr | A bitmask specifying how the search is conducted |
GetFullNameWithGenericArguments(Type)
Returns a human-readable, assembly qualified name for a class with its assembly qualified generic arguments filled in
Declaration
public static string GetFullNameWithGenericArguments(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type to get a name for |
Returns
Type | Description |
---|---|
String | The human-readable name |
GetGenericInterfaces(Type, Type, List<Type>)
Search through all interfaces implemented by this type and, if any of them match the given generic interface append them to a list
Declaration
public static void GetGenericInterfaces(this Type type, Type genericInterface, List<Type> interfaces)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type whose interfaces will be searched |
Type | genericInterface | The generic interface used to match implemented interfaces |
List<Type> | interfaces | The list to which generic interfaces will be appended |
GetImplementationsOfInterface(Type, List<Type>)
Find all types that implement the given interface type, and append them to a list If the input type is not an interface type, no action is taken.
Declaration
public static void GetImplementationsOfInterface(this Type type, List<Type> list)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The interface type whose implementors will be found |
List<Type> | list | The list to which implementors will be appended |
GetInterfaceFieldsFromClasses(IEnumerable<Type>, List<FieldInfo>, List<Type>, BindingFlags)
Gets the field info on a collection of classes that are from a collection of interfaces.
Declaration
public static void GetInterfaceFieldsFromClasses(this IEnumerable<Type> classes, List<FieldInfo> fields, List<Type> interfaceTypes, BindingFlags bindingAttr)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Type> | classes | Collection of classes to get fields from. |
List<FieldInfo> | fields | A list to which matching fields will be added |
List<Type> | interfaceTypes | Collection of interfaceTypes to check if field type implements any interface type. |
BindingFlags | bindingAttr | Binding flags of fields. |
GetMethodRecursively(Type, String, BindingFlags)
Searches this type and all base types for a method by name
Declaration
public static MethodInfo GetMethodRecursively(this Type type, string name, BindingFlags bindingAttr)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type being searched |
String | name | The name of the method for which to search |
BindingFlags | bindingAttr | BindingFlags passed to Type.GetMethod |
Returns
Type | Description |
---|---|
MethodInfo | MethodInfo for the first matching method found. Null if no method is found |
GetNameWithFullGenericArguments(Type)
Returns a human-readable name for a class with its assembly qualified generic arguments filled in
Declaration
public static string GetNameWithFullGenericArguments(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type to get a name for |
Returns
Type | Description |
---|---|
String | The human-readable name |
GetNameWithGenericArguments(Type)
Returns a human-readable name for a class with its generic arguments filled in
Declaration
public static string GetNameWithGenericArguments(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type to get a name for |
Returns
Type | Description |
---|---|
String | The human-readable name |
GetPropertiesRecursively(Type, List<PropertyInfo>, BindingFlags)
Gets all properties of the Type or any of its base Types
Declaration
public static void GetPropertiesRecursively(this Type type, List<PropertyInfo> fields, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
Type | type | Type we are going to get properties on |
List<PropertyInfo> | fields | A list to which all properties of this type will be added |
BindingFlags | bindingAttr | A bitmask specifying how the search is conducted |
GetPropertyRecursively(Type, String, BindingFlags)
Gets a specific property of the Type or any of its base Types
Declaration
public static PropertyInfo GetPropertyRecursively(this Type type, string name, BindingFlags bindingAttr)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type which will be searched for fields |
String | name | Name of the property to get |
BindingFlags | bindingAttr | A bitmask specifying how the search is conducted |
Returns
Type | Description |
---|---|
PropertyInfo | An object representing the field that matches the specified requirements, if found; otherwise, null |
IsAssignableFromOrSubclassOf(Type, Type)
Tests if class type IsAssignableFrom or IsSubclassOf another type
Declaration
public static bool IsAssignableFromOrSubclassOf(this Type checkType, Type baseType)
Parameters
Type | Name | Description |
---|---|---|
Type | checkType | type wanting to check |
Type | baseType | type wanting to check against |
Returns
Type | Description |
---|---|
Boolean | True if IsAssignableFrom or IsSubclassOf |
IsDefinedGetInheritedTypes<TAttribute>(Type, List<Type>)
Returns an array of types from the current type back to the declaring type that includes an inherited attribute.
Declaration
public static void IsDefinedGetInheritedTypes<TAttribute>(this Type type, List<Type> types)
where TAttribute : Attribute
Parameters
Type | Name | Description |
---|---|---|
Type | type | Type that has the attribute or inherits the attribute. |
List<Type> | types | A list to which matching types will be added |
Type Parameters
Name | Description |
---|---|
TAttribute | Type of attribute we are checking if is defined. |