Class TypeExtensions
Provides extension methods for System.Type.
Inheritance
Namespace: Umbraco.Extensions
Assembly: Umbraco.Core.dll
Syntax
public static class TypeExtensions
Methods
View SourceAllMethods(Type)
Gets all methods from the type and its interfaces.
Declaration
public static IEnumerable<MethodInfo> AllMethods(this Type target)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | target | The type to get methods from. |
Returns
| Type | Description |
|---|---|
| IEnumerable<MethodInfo> | An enumerable of all methods. |
FirstAttribute<TAttribute>(PropertyInfo)
Gets the first attribute of the specified type from the property.
Declaration
public static TAttribute? FirstAttribute<TAttribute>(this PropertyInfo propertyInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| PropertyInfo | propertyInfo | The property to get the attribute from. |
Returns
| Type | Description |
|---|---|
| TAttribute | The first attribute of the specified type, or null if not found. |
Type Parameters
| Name | Description |
|---|---|
| TAttribute | The type of attribute to get. |
FirstAttribute<TAttribute>(PropertyInfo, bool)
Gets the first attribute of the specified type from the property.
Declaration
public static TAttribute? FirstAttribute<TAttribute>(this PropertyInfo propertyInfo, bool inherit)
Parameters
| Type | Name | Description |
|---|---|---|
| PropertyInfo | propertyInfo | The property to get the attribute from. |
| bool | inherit | Whether to search the inheritance chain. |
Returns
| Type | Description |
|---|---|
| TAttribute | The first attribute of the specified type, or null if not found. |
Type Parameters
| Name | Description |
|---|---|
| TAttribute | The type of attribute to get. |
FirstAttribute<TAttribute>(Type)
Gets the first attribute of the specified type from the type.
Declaration
public static TAttribute? FirstAttribute<TAttribute>(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type to get the attribute from. |
Returns
| Type | Description |
|---|---|
| TAttribute | The first attribute of the specified type, or null if not found. |
Type Parameters
| Name | Description |
|---|---|
| TAttribute | The type of attribute to get. |
FirstAttribute<TAttribute>(Type, bool)
Gets the first attribute of the specified type from the type.
Declaration
public static TAttribute? FirstAttribute<TAttribute>(this Type type, bool inherit)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type to get the attribute from. |
| bool | inherit | Whether to search the inheritance chain. |
Returns
| Type | Description |
|---|---|
| TAttribute | The first attribute of the specified type, or null if not found. |
Type Parameters
| Name | Description |
|---|---|
| TAttribute | The type of attribute to get. |
GetAllMethods(Type)
Returns all methods including inherited methods even for interfaces
Declaration
public static MethodInfo[] GetAllMethods(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type |
Returns
| Type | Description |
|---|---|
| MethodInfo[] |
Remarks
Includes both Public and Non-Public methods
GetAllProperties(Type)
Gets all properties in a flat hierarchy
Declaration
public static PropertyInfo[] GetAllProperties(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type |
Returns
| Type | Description |
|---|---|
| PropertyInfo[] |
Remarks
Includes both Public and Non-Public properties
GetBaseTypes(Type?, bool)
Gets all base types of the specified type.
Declaration
public static IEnumerable<Type?> GetBaseTypes(this Type? type, bool andSelf)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type to get base types for. |
| bool | andSelf | If |
Returns
| Type | Description |
|---|---|
| IEnumerable<Type> | An enumerable of base types. |
GetCustomAttribute<T>(Type, bool)
Gets a custom attribute of the specified type from the type.
Declaration
public static T? GetCustomAttribute<T>(this Type type, bool inherit) where T : Attribute
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type to get the attribute from. |
| bool | inherit | Whether to search the inheritance chain. |
Returns
| Type | Description |
|---|---|
| T | The custom attribute, or null if not found. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of attribute to get. |
GetCustomAttributes<T>(Type?, bool)
Gets all custom attributes of the specified type from the type.
Declaration
public static IEnumerable<T> GetCustomAttributes<T>(this Type? type, bool inherited) where T : Attribute
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type to get the attributes from. |
| bool | inherited | Whether to search the inheritance chain. |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> | An enumerable of custom attributes. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of attributes to get. |
GetDefaultValue(Type)
Gets the default value for the specified type.
Declaration
public static object? GetDefaultValue(this Type t)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | t | The type to get the default value for. |
Returns
| Type | Description |
|---|---|
| object | The default value for the type (null for reference types, the default instance for value types). |
GetEnumeratedType(Type)
If the given type is an array or some other collection
comprised of 0 or more instances of a "subtype", get that type
Declaration
public static Type? GetEnumeratedType(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | the source type |
Returns
| Type | Description |
|---|---|
| Type |
GetFullNameWithAssembly(Type)
Returns the full type name with the assembly but without all of the assembly specific version information.
Declaration
public static string GetFullNameWithAssembly(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type |
Returns
| Type | Description |
|---|---|
| string |
Remarks
This method is like an 'in between' of Type.FullName and Type.AssemblyQualifiedName which returns the type and the assembly separated by a comma.
Examples
The output of this class would be: Umbraco.Core.TypeExtensions, Umbraco.Core
View SourceGetPublicMethods(Type)
Returns public methods including inherited methods even for interfaces
Declaration
public static MethodInfo[] GetPublicMethods(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type |
Returns
| Type | Description |
|---|---|
| MethodInfo[] |
GetPublicProperties(Type)
Returns public properties including inherited properties even for interfaces
Declaration
public static PropertyInfo[] GetPublicProperties(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type |
Returns
| Type | Description |
|---|---|
| PropertyInfo[] |
HasCustomAttribute<T>(Type, bool)
Determines whether the type has a custom attribute of the specified type.
Declaration
public static bool HasCustomAttribute<T>(this Type type, bool inherit) where T : Attribute
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type to check. |
| bool | inherit | Whether to search the inheritance chain. |
Returns
| Type | Description |
|---|---|
| bool |
|
Type Parameters
| Name | Description |
|---|---|
| T | The type of attribute to check for. |
Implements<TInterface>(Type)
Determines whether the type implements the specified interface.
Declaration
public static bool Implements<TInterface>(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
Type Parameters
| Name | Description |
|---|---|
| TInterface | The interface type to check. |
Inherits(Type, Type)
Determines whether the type inherits from the specified base type.
Declaration
public static bool Inherits(this Type type, Type tbase)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type to check. |
| Type | tbase | The base type to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
Inherits<TBase>(Type)
Determines whether the type inherits from the specified base type.
Declaration
public static bool Inherits<TBase>(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
Type Parameters
| Name | Description |
|---|---|
| TBase | The base type to check. |
IsAnonymousType(Type)
Checks if the type is an anonymous type
Declaration
public static bool IsAnonymousType(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type |
Returns
| Type | Description |
|---|---|
| bool |
Remarks
View SourceIsAssignableFromGtd(Type, Type)
Determines whether an instance of a specified type can be assigned to the current type instance.
Declaration
public static bool IsAssignableFromGtd(this Type type, Type c)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The current type. |
| Type | c | The type to compare with the current type. |
Returns
| Type | Description |
|---|---|
| bool | A value indicating whether an instance of the specified type can be assigned to the current type instance. |
Remarks
This extended version supports the current type being a generic type definition, and will
consider that eg List{int} is "assignable to" IList{}.
IsEnumerable(Type)
Determines whether the specified type implements System.Collections.IEnumerable.
Declaration
public static bool IsEnumerable(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
IsOfGenericType(Type, Type)
Determines whether [is of generic type] [the specified type].
Declaration
public static bool IsOfGenericType(this Type type, Type genericType)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type. |
| Type | genericType | Type of the generic. |
Returns
| Type | Description |
|---|---|
| bool |
|
IsType<T>(Type)
Determines whether the specified actual type is type.
Declaration
public static bool IsType<T>(this Type actualType)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | actualType | The actual type. |
Returns
| Type | Description |
|---|---|
| bool |
|
Type Parameters
| Name | Description |
|---|---|
| T |
MultipleAttribute<TAttribute>(PropertyInfo)
Gets all attributes of the specified type from the property.
Declaration
public static IEnumerable<TAttribute>? MultipleAttribute<TAttribute>(this PropertyInfo propertyInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| PropertyInfo | propertyInfo | The property to get the attributes from. |
Returns
| Type | Description |
|---|---|
| IEnumerable<TAttribute> | An enumerable of attributes, or null if none found. |
Type Parameters
| Name | Description |
|---|---|
| TAttribute | The type of attribute to get. |
MultipleAttribute<TAttribute>(PropertyInfo, bool)
Gets all attributes of the specified type from the property.
Declaration
public static IEnumerable<TAttribute>? MultipleAttribute<TAttribute>(this PropertyInfo propertyInfo, bool inherit)
Parameters
| Type | Name | Description |
|---|---|---|
| PropertyInfo | propertyInfo | The property to get the attributes from. |
| bool | inherit | Whether to search the inheritance chain. |
Returns
| Type | Description |
|---|---|
| IEnumerable<TAttribute> | An enumerable of attributes, or null if none found. |
Type Parameters
| Name | Description |
|---|---|
| TAttribute | The type of attribute to get. |
TryGetGenericArguments(Type, Type, out Type[]?)
Will find the generic type of the 'type' parameter passed in that is equal to the 'genericType' parameter passed in
Declaration
public static bool TryGetGenericArguments(this Type type, Type genericType, out Type[]? genericArgType)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | |
| Type | genericType | |
| Type[] | genericArgType |
Returns
| Type | Description |
|---|---|
| bool |