Class TypeExtensions
Various extension methods for type reflection.
Namespace: DotNext.Reflection
Assembly: DotNext.dll
Syntax
public static class TypeExtensions : object
Methods
| Improve this Doc 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 | The type cast result. |
Object | obj | The object to be cast. |
Returns
Type | Description |
---|---|
Object | The object after casting, or null if |
Devirtualize(Type, MethodInfo)
Returns method that overrides the specified method.
Declaration
public static MethodInfo Devirtualize(this Type type, MethodInfo abstractMethod)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type that contains overridden method. |
MethodInfo | abstractMethod | The abstract method definition. |
Returns
Type | Description |
---|---|
MethodInfo | The method that overrides |
GetBaseTypes(Type, Boolean, Boolean)
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 | The type to be discovered. |
Boolean | includeTopLevel | true to return |
Boolean | 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 |
Returns
Type | Description |
---|---|
Type[] |
Examples
var elementTypes = typeof(byte[]).IsGenericInstanceOf(typeof(IEnumerable<>));
elementTypes[0] == typeof(byte); //true
|
Improve this Doc
View Source
GetMethod(Type, String, BindingFlags, Int64, Type[])
Searches for the generic method in the specified type.
Declaration
public static MethodInfo GetMethod(this Type type, string methodName, BindingFlags flags, long genericParamCount, params Type[] parameters)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type in which search should be performed. |
String | methodName | The name of the method to get. |
BindingFlags | flags | A bitmask that specify how the search is conducted. |
Int64 | genericParamCount | Number of generic parameters in the method signature. |
Type[] | parameters | An array representing the number, order, and type of the parameters for the method to get. |
Returns
Type | Description |
---|---|
MethodInfo | Search result; or null if search criteria is invalid or method doesn't exist. |
Remarks
Element of the array parameters
should be null if this parameter of generic type.
GetMethod(Type, String, BindingFlags, Type[])
Searches for the specified method whose parameters match the specified argument types, using the specified binding constraints.
Declaration
public static MethodInfo GetMethod(this Type type, string name, BindingFlags flags, params Type[] parameters)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type in which search should be performed. |
String | name | The name of the method to get. |
BindingFlags | flags | A bitmask that specify how the search is conducted. |
Type[] | parameters | An array representing the number, order, and type of the parameters for the method to get. |
Returns
Type | Description |
---|---|
MethodInfo | Search result; or null if search criteria is invalid or method doesn't exist. |
IsAssignableFromWithoutBoxing(Type, Type)
Indicates that object of one type can be implicitly converted into another without boxing.
Declaration
public static bool IsAssignableFromWithoutBoxing(this Type to, Type from)
Parameters
Type | Name | Description |
---|---|---|
Type | to | Type of conversion result. |
Type | from | The type check. |
Returns
Type | Description |
---|---|
Boolean | 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
|
Improve this Doc
View Source
IsGenericInstanceOf(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 | The type to check. |
Type | genericDefinition | Generic type definition. |
Returns
Type | Description |
---|---|
Boolean | 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
|
Improve this Doc
View Source
IsImmutable(Type)
Determines whether the type is read-only (immutable) value type.
Declaration
public static bool IsImmutable(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type to inspect. |
Returns
Type | Description |
---|---|
Boolean | true, if the specified type is immutable value type; otherwise, false. |
IsUnmanaged(Type)
Determines whether the type is unmanaged value type.
Declaration
public static bool IsUnmanaged(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type to inspect. |
Returns
Type | Description |
---|---|
Boolean | true, if the specified type is unmanaged value type; otherwise, false. |