Class TypeHelper
A utility class for type checking, this provides internal caching so that calls to these methods will be faster than doing a manual type check in c#
Inheritance
Namespace: Umbraco.Cms.Core.Composing
Assembly: Umbraco.Core.dll
Syntax
public static class TypeHelper
Methods
View SourceCachedDiscoverableFields(Type)
Gets (and caches) System.Reflection.FieldInfo discoverable in the current System.AppDomain for a given
type.
Declaration
public static FieldInfo[] CachedDiscoverableFields(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The source. |
Returns
| Type | Description |
|---|---|
| FieldInfo[] |
CachedDiscoverableProperties(Type, bool, bool, bool)
Gets (and caches) System.Reflection.PropertyInfo discoverable in the current System.AppDomain for a given
type.
Declaration
public static PropertyInfo[] CachedDiscoverableProperties(Type type, bool mustRead = true, bool mustWrite = true, bool includeIndexed = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The source. |
| bool | mustRead | true if the properties discovered are readable |
| bool | mustWrite | true if the properties discovered are writable |
| bool | includeIndexed | true if the properties discovered are indexable |
Returns
| Type | Description |
|---|---|
| PropertyInfo[] |
CreateGenericEnumerableFromObject(object?)
Based on a type we'll check if it is IEnumerable{T} (or similar) and if so we'll return a List{T}, this will also deal with array types and return List{T} for those too. If it cannot be done, null is returned.
Declaration
public static IList? CreateGenericEnumerableFromObject(object? obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj |
Returns
| Type | Description |
|---|---|
| IList |
GetLowestBaseType(params Type[])
Finds a lowest base class amongst a collection of types
Declaration
public static Attempt<Type?> GetLowestBaseType(params Type[] types)
Parameters
| Type | Name | Description |
|---|---|---|
| Type[] | types |
Returns
| Type | Description |
|---|---|
| Attempt<Type> |
Remarks
The term 'lowest' refers to the most base class of the type collection. If a base type is not found amongst the type collection then an invalid attempt is returned.
GetProperty(Type, string, bool, bool, bool, bool)
Returns (and caches) a PropertyInfo from a type
Declaration
public static PropertyInfo? GetProperty(Type type, string name, bool mustRead = true, bool mustWrite = true, bool includeIndexed = false, bool caseSensitive = true)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | |
| string | name | |
| bool | mustRead | |
| bool | mustWrite | |
| bool | includeIndexed | |
| bool | caseSensitive |
Returns
| Type | Description |
|---|---|
| PropertyInfo |
GetReferencingAssemblies(Assembly, IEnumerable<Assembly>)
Find all assembly references that are referencing the assignTypeFrom Type's assembly found in the assemblyList
Declaration
public static IReadOnlyList<Assembly> GetReferencingAssemblies(Assembly assembly, IEnumerable<Assembly> assemblies)
Parameters
| Type | Name | Description |
|---|---|---|
| Assembly | assembly | The referenced assembly. |
| IEnumerable<Assembly> | assemblies | A list of assemblies. |
Returns
| Type | Description |
|---|---|
| IReadOnlyList<Assembly> |
Remarks
If the assembly of the assignTypeFrom Type is in the App_Code assembly, then we return nothing since things cannot reference that assembly, same with the global.asax assembly.
HasReference(Assembly, string)
Determines if an assembly references another assembly.
Declaration
public static bool HasReference(Assembly assembly, string name)
Parameters
| Type | Name | Description |
|---|---|---|
| Assembly | assembly | |
| string | name |
Returns
| Type | Description |
|---|---|
| bool |
IsImplicitValueType(Type)
A method to determine whether implementation is an implied value type (
System.Type.IsValueType, System.Type.IsEnum or a string).
Declaration
public static bool IsImplicitValueType(Type implementation)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | implementation | The implementation. |
Returns
| Type | Description |
|---|---|
| bool |
IsNonStaticClass(Type)
Returns true if the type is a class and is not static
Declaration
public static bool IsNonStaticClass(Type t)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | t |
Returns
| Type | Description |
|---|---|
| bool |
IsOverride(MethodInfo)
Checks if the method is actually overriding a base method
Declaration
public static bool IsOverride(MethodInfo m)
Parameters
| Type | Name | Description |
|---|---|---|
| MethodInfo | m |
Returns
| Type | Description |
|---|---|
| bool |
IsStaticClass(Type)
Returns true if the type is a static class
Declaration
public static bool IsStaticClass(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type |
Returns
| Type | Description |
|---|---|
| bool |
Remarks
In IL a static class is abstract and sealed see: http://stackoverflow.com/questions/1175888/determine-if-a-type-is-static
IsTypeAssignableFrom(Type, Type?)
Determines whether the type implementation is assignable from the specified implementation,
and caches the result across the application using a System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>.
Declaration
public static bool IsTypeAssignableFrom(Type contract, Type? implementation)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | contract | The type of the contract. |
| Type | implementation | The implementation. |
Returns
| Type | Description |
|---|---|
| bool |
|
IsTypeAssignableFrom<TContract>(object)
Determines whether the object instance implementation is assignable from the specified
implementation TContract,
and caches the result across the application using a System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>.
Declaration
public static bool IsTypeAssignableFrom<TContract>(object implementation)
Parameters
| Type | Name | Description |
|---|---|---|
| object | implementation | The implementation. |
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| TContract | The type of the contract. |
IsTypeAssignableFrom<TContract>(Type)
Determines whether the type implementation is assignable from the specified implementation
TContract,
and caches the result across the application using a System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>.
Declaration
public static bool IsTypeAssignableFrom<TContract>(Type implementation)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | implementation | The implementation. |
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| TContract | The type of the contract. |
IsValueType(Type)
A method to determine whether implementation represents a value type.
Declaration
public static bool IsValueType(Type implementation)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | implementation | The implementation. |
Returns
| Type | Description |
|---|---|
| bool |
MatchType(Type, Type)
Determines whether an implementation type matches a contract type, supporting generic types and variance.
Declaration
public static bool MatchType(Type implementation, Type contract)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | implementation | The implementation type to check. |
| Type | contract | The contract type to match against. |
Returns
| Type | Description |
|---|---|
| bool |
|
MatchType(Type, Type, IDictionary<string, Type>, bool)
Determines whether an implementation type matches a contract type, supporting generic types and variance.
Declaration
public static bool MatchType(Type implementation, Type contract, IDictionary<string, Type> bindings, bool variance = true)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | implementation | The implementation type to check. |
| Type | contract | The contract type to match against. |
| IDictionary<string, Type> | bindings | A dictionary to track generic parameter bindings during matching. |
| bool | variance | If |
Returns
| Type | Description |
|---|---|
| bool |
|