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
- concreteTypeType
- Concrete type to extract generics from 
- genericTypeType
- 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
concreteTypegenericTypeAn exception will be thrown if the types are incompatible.
To avoid this, first call <xref href="ActivityPub.Types.Internal.TypeExtensions.IsAssignableToGenericType(System.Type%2cSystem.Type)" data-throw-if-not-resolved="false"></xref>.
Exceptions
- ArgumentException
- If 
 does not derive from- concreteType- genericType
- ArgumentException
- If 
 is not an open generic type- genericType
GetRequiredMethod(Type, string, BindingFlags)
internal static MethodInfo GetRequiredMethod(this Type type, string methodName, BindingFlags bindingFlags = BindingFlags.Default)Parameters
- typeType
- methodNamestring
- bindingFlagsBindingFlags
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 type- genericType
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
- concreteTypeType
- Concrete type that extends from - genericType
- genericTypeType
- Open generic type 
- argumentsType[]
- Array of types used to fill 
 slots in- genericType'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)