Show / Hide Table of Contents

Class TypeExtensions

Extensions for all elements of type Type.

Inheritance
object
TypeExtensions
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: TheXDS.MCART.Types.Extensions
Assembly: MCART.dll
Syntax
public static class TypeExtensions

Methods

| Edit this page View Source

AreAllAssignable(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 source, false otherwise.

| Edit this page View Source

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 source, false otherwise.

| Edit this page View Source

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 source.

| Edit this page View Source

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 source.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

Derivates(Type, IEnumerable<Assembly>)

Enumerates the descendant types of a type within the specified assemblies.

Declaration
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.

| Edit this page View Source

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.

| Edit this page View Source

FindDerivedTypes(Type, AppDomain)

Enumerates the descendant types of a type within the specified domain.

Declaration
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.

| Edit this page View Source

FindDerivedTypes(Type, params Assembly[])

Enumerates the descendant types of a specified type within given assemblies.

Declaration
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.

| Edit this page View Source

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 T with associated data in the type's declaration; or null if the specified attribute is not found.

Type Parameters
Name Description
T

Attribute type to retrieve. Must inherit Attribute.

| Edit this page View Source

GetCollectionType(Type)

Gets the type of elements contained by a collection type.

Declaration
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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

GetDerivedTypes(Type)

Enumerates the descendant types of a specified type.

Declaration
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.

| Edit this page View Source

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.

| Edit this page View Source

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

true if the member has the attribute, false otherwise.

Type Parameters
Name Description
T

Attribute type to retrieve. Must inherit Attribute.

| Edit this page View Source

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 T has been found, it is returned. It will return null if the member does not have the specified attribute.

Returns
Type Description
bool

true if the member has the attribute, false otherwise.

Type Parameters
Name Description
T

Attribute type to retrieve. Must inherit Attribute.

| Edit this page View Source

Implements(Type, IEnumerable<Type>)

Determines if a type implements all specified types.

Declaration
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 type inherits from.

Returns
Type Description
bool

true if type implements all specified types, false otherwise.

| Edit this page View Source

Implements(Type, Type)

Determines if a type implements baseType.

Declaration
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

true if type implements baseType, false otherwise.

| Edit this page View Source

Implements(Type, Type, params Type[])

Determines if a type implements baseType with the specified generic type arguments.

Declaration
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

true if type implements baseType, false otherwise.

| Edit this page View Source

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

true if the operator exists in the type, false otherwise.

| Edit this page View Source

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

true if type implements T, false otherwise.

Type Parameters
Name Description
T

Base type inheritance to verify.

| Edit this page View Source

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 source can be assigned to one of the specified types, false otherwise.

| Edit this page View Source

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 source can be assigned to one of the specified types, false otherwise.

| Edit this page View Source

IsCollectionType(Type)

Determines if the type refers to a collection type.

Declaration
public static bool IsCollectionType(this Type type)
Parameters
Type Name Description
Type type

Type to check.

Returns
Type Description
bool

true if the type is a collection type, false otherwise.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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

true if t is a numeric type; otherwise, false.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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 type is null.

ClassNotInstantiableException

Occurs if the type type cannot be instantiated using a public parameterless constructor.

| Edit this page View Source

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 throwOnFail is false.

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 throwOnFail is set to true.

ArgumentNullException

Occurs if type is null and throwOnFail is set to true.

ClassNotInstantiableException

Occurs if the type type cannot be instantiated using a public parameterless constructor and throwOnFail is set to true.

| Edit this page View Source

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 throwOnFail is false.

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 throwOnFail is set to true.

ArgumentNullException

Occurs if type is null and throwOnFail is set to true.

ClassNotInstantiableException

Occurs if the type type cannot be instantiated using a public constructor that accepts the parameters specified in parameters and throwOnFail is set to true.

| Edit this page View Source

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 type is null.

ClassNotInstantiableException

Occurs if the type type cannot be instantiated using a public constructor that accepts the parameters specified in parameters.

| Edit this page View Source

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 T.

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 type is null.

ClassNotInstantiableException

Occurs if the type type cannot be instantiated using a public constructor without parameters.

| Edit this page View Source

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 T.

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 throwOnFail is false.

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 throwOnFail is set to true.

ArgumentNullException

Occurs if type is null and throwOnFail is set to true.

ClassNotInstantiableException

Occurs if the type type cannot be instantiated using a public constructor and throwOnFail is set to true.

| Edit this page View Source

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 T.

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 throwOnFail is false.

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 throwOnFail is set to true.

ArgumentNullException

Occurs if type is null and throwOnFail is set to true.

ClassNotInstantiableException

Occurs if the type type cannot be instantiated using a public constructor that accepts the parameters specified in parameters and throwOnFail is set to true.

| Edit this page View Source

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 T.

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 type is null.

ClassNotInstantiableException

Occurs if the type type cannot be instantiated using a public constructor that accepts the parameters specified in parameters.

| Edit this page View Source

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 T

Returns
Type Description
T

The new instance of the specified type.

Type Parameters
Name Description
T

Type of instance to return.

| Edit this page View Source

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 T.

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 throwOnFail is false.

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 type is null and throwOnFail is true.

ClassNotInstantiableException

Occurs if the type type cannot be instantiated using a public constructor that accepts the parameters specified in parameters and throwOnFail is true.

| Edit this page View Source

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 T.

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.

| Edit this page View Source

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 t if the type is not nullable.

| Edit this page View Source

ResolveCollectionType(Type)

Resolves a collection type to the type of its elements.

Declaration
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 type, or type if it is not a collection type.

| Edit this page View Source

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

t, if it's a type defined at compile time, or a base type that is. It will return null if there is no base type defined, like in interfaces.

| Edit this page View Source

ToNamedEnum(Type)

Converts the values of an enumeration type to a collection of NamedObject<T>.

Declaration
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 type is null.

InvalidTypeException

Occurs if type is not an enumeration type.

| Edit this page View Source

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.

| Edit this page View Source

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.

  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX