Class TypeExtensions
Extensions for all elements of type Type.
Inherited Members
Namespace: TheXDS.MCART.Types.Extensions
Assembly: MCART.dll
Syntax
public static class TypeExtensions
Methods
| Edit this page View SourceAreAllAssignable(Type, IEnumerable<Type>)
Checks if all types are assignable from the source type.
Declaration
public static bool AreAllAssignable(this Type source, IEnumerable<Type> types)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | source | Type that is being assigned. |
| IEnumerable<Type> | types | List of types to check. |
Returns
| Type | Description |
|---|---|
| bool | true if all types are assignable from
|
AreAllAssignable(Type, params Type[])
Checks if all types are assignable from the source type.
Declaration
public static bool AreAllAssignable(this Type source, params Type[] types)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | source | Type that is being assigned. |
| Type[] | types | List of types to check. |
Returns
| Type | Description |
|---|---|
| bool | true if all types are assignable from
|
Assignables(Type, IEnumerable<Type>)
Enumerates the types that are assignable from source.
Declaration
public static IEnumerable<Type> Assignables(this Type source, IEnumerable<Type> types)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | source | Type that is being assigned. |
| IEnumerable<Type> | types | List of types to check. |
Returns
| Type | Description |
|---|---|
| IEnumerable<Type> | An enumerator with the types that can be assigned from
|
Assignables(Type, params Type[])
Enumerates the types that are assignable from source.
Declaration
public static IEnumerable<Type> Assignables(this Type source, params Type[] types)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | source | Type that is being assigned. |
| Type[] | types | List of types to check. |
Returns
| Type | Description |
|---|---|
| IEnumerable<Type> | An enumerator with the types that can be assigned from
|
CSharpName(Type)
Gets the type name as it would be declared in C#.
Declaration
public static string CSharpName(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type from which to obtain the declaration string. |
Returns
| Type | Description |
|---|---|
| string | A string representing the type's declaration using C# syntax. |
Remarks
The types will use their full name, including their namespace. Types with reserved keyword aliases (like int or string) will use their type name instead.
CleanFullName(Type)
Gets the full name of the type without including the generic argument count annotation.
Declaration
public static string CleanFullName(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type from which to obtain the clean name. |
Returns
| Type | Description |
|---|---|
| string | The full type name, including its namespace, but not its assembly or its generic argument count annotation if present. |
Default(Type)
Programmatic equivalent of default, gets the default value of a type.
Declaration
public static object? Default(this Type t)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | t | Type from which to obtain the default value. |
Returns
| Type | Description |
|---|---|
| object | A new instance of the type if it is a struct, or null if it's a class. |
Derivates(Type, IEnumerable<Assembly>)
Enumerates the descendant types of a type within the specified assemblies.
Declaration
[RequiresUnreferencedCode("The method obtains a collection of types without direct references, so it is not compatible with trimming.")]
public static IEnumerable<Type> Derivates(this Type type, IEnumerable<Assembly> assemblies)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type for which to find descendants. |
| IEnumerable<Assembly> | assemblies | Sequence containing a list of assemblies in which to perform the search. |
Returns
| Type | Description |
|---|---|
| IEnumerable<Type> | A sequence with all descendant types of the specified type. |
Derivates(Type, IEnumerable<Type>)
Enumerates the descendant types of a type within the specified collection of types.
Declaration
public static IEnumerable<Type> Derivates(this Type type, IEnumerable<Type> types)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type for which to find descendants. |
| IEnumerable<Type> | types | Sequence containing a list of types in which to perform the search. |
Returns
| Type | Description |
|---|---|
| IEnumerable<Type> | A sequence with all descendant types of the specified type. |
FindDerivedTypes(Type, AppDomain)
Enumerates the descendant types of a type within the specified domain.
Declaration
[RequiresUnreferencedCode("The method obtains a collection of types without direct references, so it is not compatible with trimming.")]
public static IEnumerable<Type> FindDerivedTypes(this Type type, AppDomain domain)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type for which to find descendants. |
| AppDomain | domain | Domain on which to perform the search. |
Returns
| Type | Description |
|---|---|
| IEnumerable<Type> | A sequence with all descendant types of the specified type. |
FindDerivedTypes(Type, params Assembly[])
Enumerates the descendant types of a specified type within given assemblies.
Declaration
[RequiresUnreferencedCode("The method obtains a collection of types without direct references, so it is not compatible with trimming.")]
public static IEnumerable<Type> FindDerivedTypes(this Type type, params Assembly[] assemblies)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type for which to find descendants. |
| Assembly[] | assemblies | Sequence containing a list of assemblies in which to perform the search. |
Returns
| Type | Description |
|---|---|
| IEnumerable<Type> | A sequence with all descendant types of the specified type. |
GetAttrAlt<T>(Type)
Returns the attribute associated with the declaration of a specified type, or otherwise, from the assembly that contains it.
Declaration
public static T? GetAttrAlt<T>(this Type type) where T : Attribute
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Object from which to extract the attribute. |
Returns
| Type | Description |
|---|---|
| T | An attribute of type |
Type Parameters
| Name | Description |
|---|---|
| T | Attribute type to retrieve. Must inherit Attribute. |
GetCollectionType(Type)
Gets the type of elements contained by a collection type.
Declaration
[RequiresDynamicCode("The method makes calls to dynamic code, so it is not compatible with trimming.")]
public static Type GetCollectionType(this Type collectionType)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | collectionType | Collection type from which to obtain the element type. |
Returns
| Type | Description |
|---|---|
| Type | The type of elements contained by the collection. |
Remarks
By convention, it is assumed that the element type of a collection is based on the generic type arguments used in its definition, with a common convention of placing the element type at the end of the type arguments.
GetDefinedMethods(Type)
Gets a collection of publicly defined instance methods directly in the type.
Declaration
public static IEnumerable<MethodInfo> GetDefinedMethods(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type for which to list the methods. |
Returns
| Type | Description |
|---|---|
| IEnumerable<MethodInfo> | An enumeration of methods defined directly in the type. |
GetDefinedMethods(Type, BindingFlags)
Gets a collection of methods defined directly in the specified type.
Declaration
public static IEnumerable<MethodInfo> GetDefinedMethods(this Type type, BindingFlags flags)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type for which to list the methods. |
| BindingFlags | flags | Flags to use for filtering the methods to retrieve. |
Returns
| Type | Description |
|---|---|
| IEnumerable<MethodInfo> | An enumeration of methods defined directly in the type. |
GetDerivedTypes(Type)
Enumerates the descendant types of a specified type.
Declaration
[RequiresUnreferencedCode("The method obtains a collection of types without direct references, so it is not compatible with trimming.")]
public static IEnumerable<Type> GetDerivedTypes(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type for which to find descendants. |
Returns
| Type | Description |
|---|---|
| IEnumerable<Type> | A sequence with all descendant types of the specified type. |
GetPublicProperties(Type)
Enumerates all public instance properties of the type.
Declaration
public static IEnumerable<PropertyInfo> GetPublicProperties(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type for which to enumerate the public instance properties. |
Returns
| Type | Description |
|---|---|
| IEnumerable<PropertyInfo> | An enumeration with all public instance properties of the type. |
HasAttrAlt<T>(Type)
Determines if a member or its containing assembly has a defined attribute.
Declaration
public static bool HasAttrAlt<T>(this Type type) where T : Attribute
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Member from which to extract the attribute. |
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| T | Attribute type to retrieve. Must inherit Attribute. |
HasAttrAlt<T>(Type, out T)
Determines if a member or its containing assembly has a defined attribute.
Declaration
public static bool HasAttrAlt<T>(this Type type, out T attribute) where T : Attribute
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Member from which to extract the attribute. |
| T | attribute | Output parameter. If an attribute of type
|
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| T | Attribute type to retrieve. Must inherit Attribute. |
Implements(Type, IEnumerable<Type>)
Determines if a type implements all specified types.
Declaration
[RequiresDynamicCode("The method dynamically creates new types, so it is not compatible with trimming.")]
[RequiresUnreferencedCode("The method dynamically creates new types, so it is not compatible with trimming.")]
public static bool Implements(this Type type, IEnumerable<Type> baseTypes)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to check |
| IEnumerable<Type> | baseTypes | Collection of types to verify that |
Returns
| Type | Description |
|---|---|
| bool | true if |
Implements(Type, Type)
Determines if a type implements baseType.
Declaration
[RequiresDynamicCode("The method dynamically creates new types, so it is not compatible with trimming.")]
[RequiresUnreferencedCode("The method dynamically creates new types, so it is not compatible with trimming.")]
public static bool Implements(this Type type, Type baseType)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to check |
| Type | baseType | Base type inheritance to verify. |
Returns
| Type | Description |
|---|---|
| bool |
Implements(Type, Type, params Type[])
Determines if a type implements baseType with the specified generic type arguments.
Declaration
[RequiresDynamicCode("The method dynamically creates new types, so it is not compatible with trimming.")]
[RequiresUnreferencedCode("The method dynamically creates new types, so it is not compatible with trimming.")]
public static bool Implements(this Type type, Type baseType, params Type[] typeArgs)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to check |
| Type | baseType | Base type inheritance to verify. |
| Type[] | typeArgs | Generic type arguments to use for creating the generic type to check. |
Returns
| Type | Description |
|---|---|
| bool |
ImplementsOperator(Type, Func<Expression, Expression, BinaryExpression>)
Checks if a type implements an operator specified by the expression.
Declaration
public static bool ImplementsOperator(this Type type, Func<Expression, Expression, BinaryExpression> @operator)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to check |
| Func<Expression, Expression, BinaryExpression> | operator | Operator to search in the type. |
Returns
| Type | Description |
|---|---|
| bool |
Implements<T>(Type)
Determines if a type implements T.
Declaration
public static bool Implements<T>(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to check |
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| T | Base type inheritance to verify. |
IsAnyAssignable(Type, IEnumerable<Type>)
Checks if any of the specified types are assignable from the type source.
Declaration
public static bool IsAnyAssignable(this Type source, IEnumerable<Type> types)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | source | Type that is desired to be assigned. |
| IEnumerable<Type> | types | List of types to check. |
Returns
| Type | Description |
|---|---|
| bool | true if the type |
IsAnyAssignable(Type, params Type[])
Checks if any of the specified types are assignable from the type source.
Declaration
public static bool IsAnyAssignable(this Type source, params Type[] types)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | source | Type that is desired to be assigned. |
| Type[] | types | List of types to check. |
Returns
| Type | Description |
|---|---|
| bool | true if the type |
IsCollectionType(Type)
Determines if the type refers to a collection type.
Declaration
[RequiresDynamicCode("The method dynamically creates new types, so it is not compatible with trimming.")]
public static bool IsCollectionType(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to check. |
Returns
| Type | Description |
|---|---|
| bool |
IsInstantiable(Type)
Gets a value that determines if the type is instantiable.
Declaration
public static bool IsInstantiable(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to check. |
Returns
| Type | Description |
|---|---|
| bool | true if the type is instantiable by means of a parameterless constructor, false otherwise. |
IsInstantiable(Type, IEnumerable<Type>?)
Gets a value that determines if the type is instantiable.
Declaration
public static bool IsInstantiable(this Type type, IEnumerable<Type>? constructorArgs)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to check. |
| IEnumerable<Type> | constructorArgs | Collection with the argument types that the constructor to search for must contain. |
Returns
| Type | Description |
|---|---|
| bool | true if the type is instantiable by means of a constructor with parameters of the specified type, false otherwise. |
IsInstantiable(Type, params Type[]?)
Gets a value that determines if the type is instantiable using a constructor that accepts the specified parameters.
Declaration
public static bool IsInstantiable(this Type type, params Type[]? constructorArgs)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to check. |
| Type[] | constructorArgs | Collection with the argument types that the constructor to search for must contain. |
Returns
| Type | Description |
|---|---|
| bool | true if the type is instantiable by means of a constructor with parameters of the specified type, false otherwise. |
IsNumericType(Type?)
Determines if the type t is a numeric type
Declaration
public static bool IsNumericType(this Type? t)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | t | Type to check |
Returns
| Type | Description |
|---|---|
| bool |
IsStruct(Type)
Gets a value that determines if the type is a non-primitive value type.
Declaration
public static bool IsStruct(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to check. |
Returns
| Type | Description |
|---|---|
| bool | true if the type is a non-primitive value type, false otherwise. |
New(Type)
Initializes a new instance of the runtime specified type.
Declaration
public static object New(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. |
Returns
| Type | Description |
|---|---|
| object | The new instance of the specified type. |
New(Type, params object?[])
Initializes a new instance of an object with a constructor that accepts the provided arguments.
Declaration
public static object New(this Type type, params object?[] parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. |
| object[] | parameters | Parameters to pass to the constructor. A compatible constructor will be searched for to create the instance. |
Returns
| Type | Description |
|---|---|
| object | The new instance of the specified type. |
NewAsync(Type)
Initializes a new instance of an object asynchronously with a parameterless constructor.
Declaration
public static Task<object?> NewAsync(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. |
Returns
| Type | Description |
|---|---|
| Task<object> | A new instance of the specified type, or null if a problem occurs while instantiating the type. |
Remarks
Using this method to create an instance asynchronously can be problematic if normal program execution depends on which thread owns the object, for example when instantiating UI elements.
Exceptions
| Type | Condition |
|---|---|
| TypeLoadException | Occurs if it's not possible to instantiate a class of the requested type. |
| ArgumentNullException | Occurs if |
| ClassNotInstantiableException | Occurs if the type |
NewAsync(Type, bool)
Initializes a new instance of an object asynchronously with a parameterless constructor.
Declaration
public static Task<object?> NewAsync(this Type type, bool throwOnFail)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. |
| bool | throwOnFail | If set to true, an exception will be thrown if the type cannot be instantiated with the provided information, otherwise returns null if set to false |
Returns
| Type | Description |
|---|---|
| Task<object> | A new instance of the specified type, or
null if a problem occurs while instantiating the
type and |
Remarks
Using this method to create an instance asynchronously can be problematic if normal program execution depends on which thread owns the object, for example when instantiating UI elements.
Exceptions
| Type | Condition |
|---|---|
| TypeLoadException | Occurs if it's not possible to instantiate a class of the requested type and
|
| ArgumentNullException | |
| ClassNotInstantiableException | Occurs if the type |
NewAsync(Type, bool, IEnumerable?)
Initializes a new instance of an object asynchronously with a constructor that accepts the provided arguments.
Declaration
public static Task<object?> NewAsync(this Type type, bool throwOnFail, IEnumerable? parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. |
| bool | throwOnFail | If set to true, an exception will be thrown if the type cannot be instantiated with the provided information, otherwise returns null if set to false. |
| IEnumerable | parameters | Parameters to pass to the constructor. A compatible constructor will be searched for to create the instance. |
Returns
| Type | Description |
|---|---|
| Task<object> | A new instance of the specified type, or
null if a problem occurs while instantiating the
type and |
Remarks
Using this method to create an instance asynchronously can be problematic if normal program execution depends on which thread owns the object, for example when instantiating UI elements.
Exceptions
| Type | Condition |
|---|---|
| TypeLoadException | Occurs if it's not possible to instantiate a class of the requested type and
|
| ArgumentNullException | |
| ClassNotInstantiableException | Occurs if the type |
NewAsync(Type, IEnumerable?)
Initializes a new instance of an object asynchronously with a constructor that accepts the provided arguments.
Declaration
public static Task<object?> NewAsync(this Type type, IEnumerable? parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. |
| IEnumerable | parameters | Parameters to pass to the constructor. A compatible constructor will be searched for to create the instance. |
Returns
| Type | Description |
|---|---|
| Task<object> | A new instance of the specified type, or null if a problem occurs while instantiating the type. |
Remarks
Using this method to create an instance asynchronously can be problematic if normal program execution depends on which thread owns the object, for example when instantiating UI elements.
Exceptions
| Type | Condition |
|---|---|
| TypeLoadException | Occurs if it's not possible to instantiate a class of the requested type. |
| ArgumentNullException | Occurs if |
| ClassNotInstantiableException | Occurs if the type |
NewAsync<T>(Type)
Initializes a new instance of an object asynchronously with a constructor that accepts the provided arguments.
Declaration
public static Task<T> NewAsync<T>(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. Must be, inherit from or implement
the type specified in |
Returns
| Type | Description |
|---|---|
| Task<T> | A new instance of the specified type. |
Type Parameters
| Name | Description |
|---|---|
| T | Type of instance to return. |
Exceptions
| Type | Condition |
|---|---|
| TypeLoadException | Occurs if it's not possible to instantiate a class of the requested type. |
| ArgumentNullException | Occurs if |
| ClassNotInstantiableException | Occurs if the type |
NewAsync<T>(Type, bool)
Initializes a new instance of an object asynchronously with a constructor that accepts the provided arguments.
Declaration
public static Task<T> NewAsync<T>(this Type type, bool throwOnFail)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. Must be, inherit from or implement
the type specified in |
| bool | throwOnFail | If set to true, an exception will be thrown if the type cannot be instantiated with the provided information, otherwise returns null if set to false |
Returns
| Type | Description |
|---|---|
| Task<T> | A new instance of the specified type, or
null if a problem occurs while instantiating the
type and |
Type Parameters
| Name | Description |
|---|---|
| T | Type of instance to return. |
Exceptions
| Type | Condition |
|---|---|
| TypeLoadException | Occurs if it's not possible to instantiate a class of the requested type and
|
| ArgumentNullException | |
| ClassNotInstantiableException | Occurs if the type |
NewAsync<T>(Type, bool, IEnumerable?)
Initializes a new instance of an object asynchronously with a constructor that accepts the provided arguments.
Declaration
public static Task<T> NewAsync<T>(this Type type, bool throwOnFail, IEnumerable? parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. Must be, inherit from or implement
the type specified in |
| bool | throwOnFail | If set to true, an exception will be thrown if the type cannot be instantiated with the provided information, otherwise returns null if set to false |
| IEnumerable | parameters | Parameters to pass to the constructor. A compatible constructor will be searched for to create the instance. |
Returns
| Type | Description |
|---|---|
| Task<T> | A new instance of the specified type, or
null if a problem occurs while instantiating the
type and |
Type Parameters
| Name | Description |
|---|---|
| T | Type of instance to return. |
Remarks
Using this method to create an instance asynchronously can be problematic if normal program execution depends on which thread owns the object, for example when instantiating UI elements.
Exceptions
| Type | Condition |
|---|---|
| TypeLoadException | Occurs if it's not possible to instantiate a class of the requested type and
|
| ArgumentNullException | |
| ClassNotInstantiableException | Occurs if the type |
NewAsync<T>(Type, IEnumerable?)
Initializes a new instance of an object asynchronously with a constructor that accepts the provided arguments.
Declaration
public static Task<T> NewAsync<T>(this Type type, IEnumerable? parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. Must be, inherit from or implement
the type specified in |
| IEnumerable | parameters | Parameters to pass to the constructor. A compatible constructor will be searched for to create the instance. |
Returns
| Type | Description |
|---|---|
| Task<T> | A new instance of the specified type, or null if a problem occurs while instantiating the type. |
Type Parameters
| Name | Description |
|---|---|
| T | Type of instance to return. |
Exceptions
| Type | Condition |
|---|---|
| TypeLoadException | Occurs if it's not possible to instantiate a class of the requested type. |
| ArgumentNullException | Occurs if |
| ClassNotInstantiableException | Occurs if the type |
New<T>(Type)
Initializes a new instance of the dynamic type specified,
returning it as a T.
Declaration
public static T New<T>(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. Must be, inherit from or implement
the type specified in |
Returns
| Type | Description |
|---|---|
| T | The new instance of the specified type. |
Type Parameters
| Name | Description |
|---|---|
| T | Type of instance to return. |
New<T>(Type, bool, IEnumerable?)
Initializes a new instance of an object with a constructor that accepts the provided arguments.
Declaration
public static T? New<T>(this Type type, bool throwOnFail, IEnumerable? parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. Must be, inherit from or implement
the type specified in |
| bool | throwOnFail | If set to true, an exception will be thrown if the type cannot be instantiated with the provided information, otherwise returns null if set to false. |
| IEnumerable | parameters | Parameters to pass to the constructor. A compatible constructor will be searched for to create the instance. |
Returns
| Type | Description |
|---|---|
| T | A new instance of the specified type, or
null if a problem occurs while instantiating the
type and |
Type Parameters
| Name | Description |
|---|---|
| T | Type of instance to return. |
Exceptions
| Type | Condition |
|---|---|
| TypeLoadException | Occurs if it's not possible to instantiate a class of the requested type. |
| ArgumentNullException | |
| ClassNotInstantiableException | Occurs if the type |
New<T>(Type, params object?[])
Initializes a new instance of an object with a constructor that accepts the provided arguments.
Declaration
public static T New<T>(this Type type, params object?[] parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to instantiate. Must be, inherit from or implement
the type specified in |
| object[] | parameters | Parameters to pass to the constructor. A compatible constructor will be searched for to create the instance. |
Returns
| Type | Description |
|---|---|
| T | A new instance of the specified type. |
Type Parameters
| Name | Description |
|---|---|
| T | Type of instance to return. |
Exceptions
| Type | Condition |
|---|---|
| TypeLoadException | Occurs if it's not possible to instantiate a class of the requested type. |
NotNullable(Type)
Ensures to return a non-nullable type for structures.
Declaration
public static Type NotNullable(this Type t)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | t | Type to return |
Returns
| Type | Description |
|---|---|
| Type | The underlying type of a Nullable<T>, or
|
ResolveCollectionType(Type)
Resolves a collection type to the type of its elements.
Declaration
[RequiresDynamicCode("The method dynamically creates new types, so it is not compatible with trimming.")]
public static Type ResolveCollectionType(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Type to check. |
Returns
| Type | Description |
|---|---|
| Type | The element type of the collection for the type
|
ResolveToDefinedType(Type)
Ensures a type defined at compile time is returned.
Declaration
public static Type? ResolveToDefinedType(this Type t)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | t | Type to check. |
Returns
| Type | Description |
|---|---|
| Type |
|
ToNamedEnum(Type)
Converts the values of an enumeration type to a collection of NamedObject<T>.
Declaration
[RequiresDynamicCode("The method makes calls to dynamic code, so it is not compatible with trimming.")]
public static IEnumerable<NamedObject<Enum>> ToNamedEnum(this Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | Enumeration type to convert. |
Returns
| Type | Description |
|---|---|
| IEnumerable<NamedObject<Enum>> | An enumeration of all NamedObject<T> created from the values of the specified enumeration type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Occurs if |
| InvalidTypeException | Occurs if |
TryInstance(Type, out object, params object[]?)
Attempts to instantiate the type with the specified constructor arguments.
Declaration
public static bool TryInstance(this Type t, out object instance, params object[]? args)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | t | Type that will be attempted to instantiate. |
| object | instance | Output parameter. Instance created or null if it was not possible to create an instance of the specified type. |
| object[] | args | Arguments to pass to the constructor. Can be omitted or set to null for constructors with no arguments. |
Returns
| Type | Description |
|---|---|
| bool | true if it was possible to instantiate the type and do so correctly, false otherwise. |
TryInstance<T>(Type, out T, params object[]?)
Attempts to instantiate the type with the specified constructor arguments,
returning it as an object of type T.
Declaration
public static bool TryInstance<T>(this Type t, out T instance, params object[]? args)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | t | Type that will be attempted to instantiate. |
| T | instance | Output parameter. Instance created or null if it was not possible to create an instance of the specified type. |
| object[] | args | Arguments to pass to the constructor. Can be omitted or set to null for constructors with no arguments. |
Returns
| Type | Description |
|---|---|
| bool | true if it was possible to instantiate the type and do so correctly, false otherwise. |
Type Parameters
| Name | Description |
|---|---|
| T | Type of object to return. |