Class TypeExtensions
Various extension methods for type reflection.
Inherited Members
Namespace: DotNext.Reflection
Assembly: DotNext.dll
Syntax
public static class TypeExtensions
Methods
View SourceCast(Type, object?)
Casts an object to the class, value type or interface.
Declaration
public static object? Cast(this Type type, object? obj)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | |
| object | obj | The object to be cast. |
Returns
| Type | Description |
|---|---|
| object |
Exceptions
| Type | Condition |
|---|---|
| InvalidCastException | If the object is not null and is not assignable to the expected type; or if object is null and the type is value type. |
Devirtualize(Type, MethodInfo)
Returns method that overrides the specified method.
Declaration
[RequiresUnreferencedCode("Dynamic code generation may be incompatible with IL trimming")]
public static MethodInfo? Devirtualize(this Type type, MethodInfo abstractMethod)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | |
| MethodInfo | abstractMethod | The abstract method definition. |
Returns
| Type | Description |
|---|---|
| MethodInfo | The method that overrides |
GetBaseTypes(Type, bool, bool)
Returns read-only collection of base types and, optionally, all implemented interfaces.
Declaration
public static IEnumerable<Type> GetBaseTypes(this Type type, bool includeTopLevel = false, bool includeInterfaces = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | |
| bool | includeTopLevel | true to return the top-level type as the first element in the collection. |
| bool | includeInterfaces | true to include implemented interfaces; false to return inheritance hierarchy only. |
Returns
| Type | Description |
|---|---|
| IEnumerable<Type> | Read-only collection of base types and, optionally, all implemented interfaces. |
GetGenericArguments(Type, Type)
Returns actual generic arguments passed into generic type definition implemented by the input type.
Declaration
public static Type[] GetGenericArguments(this Type type, Type genericDefinition)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | |
| Type | genericDefinition | The definition of generic type. |
Returns
| Type | Description |
|---|---|
| Type[] | The array of actual generic types required by |
Examples
var elementTypes = typeof(byte[]).IsGenericInstanceOf(typeof(IEnumerable<>));
elementTypes[0] == typeof(byte); //true
View Source
IsAssignableFromWithoutBoxing(Type, Type)
Indicates that object of one type can be implicitly converted into another without boxing.
Declaration
public static bool IsAssignableFromWithoutBoxing(this Type type, Type from)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | |
| Type | from | The type check. |
Returns
| Type | Description |
|---|---|
| bool | true if |
Examples
typeof(object).IsAssignableFrom(typeof(int)); //true
typeof(object).IsAssignableFromWithoutBoxing(typeof(int)); //false
typeof(object).IsAssignableFrom(typeof(string)); //true
typeof(object).IsAssignableFromWithoutBoxing(typeof(string));//true
See Also
View SourceIsGenericInstanceOf(Type, Type)
Determines whether the type is an instance of the specified generic type.
Declaration
public static bool IsGenericInstanceOf(this Type type, Type genericDefinition)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | |
| Type | genericDefinition | Generic type definition. |
Returns
| Type | Description |
|---|---|
| bool | true, if the type is an instance of the specified generic type; otherwise, false. |
Examples
typeof(byte[]).IsGenericInstanceOf(typeof(IEnumerable<>)); //returns true
typeof(List<int>).IsGenericInstanceOf(typeof(List<int>)); //returns true
View Source
get_DefaultValue(Type)
Gets default value for the specified type.
Declaration
public static object? get_DefaultValue(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type |
Returns
| Type | Description |
|---|---|
| object | The default value of type |
Remarks
The method returns null for all reference and pointer types and default boxed value for value types.
get_IsImmutable(Type)
Determines whether the type is read-only (immutable) value type.
Declaration
public static bool get_IsImmutable(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type |
Returns
| Type | Description |
|---|---|
| bool | true, if the specified type is immutable value type; otherwise, false. |
get_IsUnmanaged(Type)
Determines whether the type is unmanaged value type.
Declaration
[RequiresUnreferencedCode("Dynamic code generation may be incompatible with IL trimming")]
public static bool get_IsUnmanaged(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type |
Returns
| Type | Description |
|---|---|
| bool | true, if the specified type is unmanaged value type; otherwise, false. |
get_IsVoid(Type)
Gets a value indicating that the type is void.
Declaration
public static bool get_IsVoid(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type |
Returns
| Type | Description |
|---|---|
| bool |