Table of Contents

Class TypeExtensions

Namespace
ActivityPub.Types.Internal
Assembly
ActivityPub.Types.dll

Internal utilities for working with .NET types reflectively

internal static class TypeExtensions
Inheritance
TypeExtensions
Inherited Members

Methods

GetGenericArgumentsFor(Type, Type)

Given a specific concrete type that derives from some generic type, returns the actual types used to fill the generic.

internal static Type[] GetGenericArgumentsFor(this Type concreteType, Type genericType)

Parameters

concreteType Type

Concrete type to extract generics from

genericType Type

Generic type containing slots to fill

Returns

Type[]

Returns an array containing the actual type of each generic slot

Examples

// Returns [ System.String ]
GetGenericArgumentsFor(typeof(List<string>), typeof(ICollection<string>))

Remarks

concreteType
must be assignable to
genericType
, but this check is skipped for performance.

An exception will be thrown if the types are incompatible. To avoid this, first call IsAssignableToGenericType(Type, Type).

Exceptions

ArgumentException

If

concreteType
does not derive from
genericType
ArgumentException

If

genericType
is not an open generic type

GetRequiredMethod(Type, string, BindingFlags)

internal static MethodInfo GetRequiredMethod(this Type type, string methodName, BindingFlags bindingFlags = BindingFlags.Default)

Parameters

type Type
methodName string
bindingFlags BindingFlags

Returns

MethodInfo

IsAssignableToGenericType(Type, Type)

Determines if a concrete type can be assigned to an open generic type. Based on https://stackoverflow.com/a/1075059/

internal static bool IsAssignableToGenericType(this Type concreteType, Type genericType)

Parameters

concreteType Type

Real, concrete type

genericType Type

Open generic type

Returns

bool

true if concreteType can be assigned to

genericType
, false otherwise

Exceptions

ArgumentException

If

genericType
is not an open generic type

TryGetGenericArgumentsFor(Type, Type, out Type[]?)

Attempts to find the concrete type parameters used to fill a generic type. Returns false and null if the types are incompatible.

internal static bool TryGetGenericArgumentsFor(this Type concreteType, Type genericType, out Type[]? arguments)

Parameters

concreteType Type

Concrete type that extends from

genericType
genericType Type

Open generic type

arguments Type[]

Array of types used to fill

genericType's
slots in
concreteType

Returns

bool

return true if parameters were found, false otherwise

Remarks

This is inefficient, but its avoids potential exceptions from GetGenericArgumentsFor(Type, Type) when the conversion fails.

See Also

TryMakeGenericType(Type, out Type?, params Type[])

internal static bool TryMakeGenericType(this Type type, out Type? genericType, params Type[] arguments)

Parameters

type Type
genericType Type
arguments Type[]

Returns

bool