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
TypeConcrete type to extract generics from
genericType
TypeGeneric 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
does not derive fromconcreteType
genericType
- ArgumentException
If
is not an open generic typegenericType
GetRequiredMethod(Type, string, BindingFlags)
internal static MethodInfo GetRequiredMethod(this Type type, string methodName, BindingFlags bindingFlags = BindingFlags.Default)
Parameters
type
TypemethodName
stringbindingFlags
BindingFlags
Returns
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
Returns
Exceptions
- ArgumentException
If
is not an open generic typegenericType
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
TypeConcrete type that extends from
genericType
genericType
TypeOpen generic type
arguments
Type[]Array of types used to fill
slots ingenericType's
concreteType
Returns
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)