Class TypeExtensions

Namespace
Cratis.Reflection
Assembly
Cratis.Fundamentals.dll

Provides a set of methods for working with types.

public static class TypeExtensions
Inheritance
TypeExtensions
Inherited Members

Methods

AllBaseAndImplementingTypes(Type)

Returns all base types of a given type, both open and closed generic (if any), including itself.

[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "Reflection-based base/interface discovery is used by compatibility fallback paths when generated metadata is unavailable.")]
public static IEnumerable<Type> AllBaseAndImplementingTypes(this Type type)

Parameters

type Type

Type to get for.

Returns

IEnumerable<Type>

All base and implementing types.

GetAsyncEnumerableElementType(Type)

Gets the element type of an IAsyncEnumerable<T>.

[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "IAsyncEnumerable<T> is a BCL interface; its interface implementations are preserved by the runtime.")]
public static Type GetAsyncEnumerableElementType(this Type asyncEnumerableType)

Parameters

asyncEnumerableType Type

The Type to get from.

Returns

Type

The element type.

GetEnumerableElementType(Type)

Gets the element type of an enumerable.

[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "IEnumerable<T> is a BCL interface; its interface implementations are preserved by the runtime.")]
public static Type GetEnumerableElementType(this Type enumerableType)

Parameters

enumerableType Type

The type of the enumerable.

Returns

Type

Enumerable element Type.

Remarks

GetNullableType(Type)

Get the underlying nullable type.

public static Type GetNullableType(this Type type)

Parameters

type Type

Type to get from.

Returns

Type

Underlying nullable type.

GetSettableProperties(Type)

Gets all the public properties with setters.

[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "Reflection on public properties is used by compatibility paths; callers are responsible for preserving required types.")]
public static PropertyInfo[] GetSettableProperties(this Type type)

Parameters

type Type

Type to get settable properties for.

Returns

PropertyInfo[]

Settable properties.

GetTypeInfoDetails(Type)

Get ITypeInfo from Type.

[RequiresDynamicCode("Uses MakeGenericType to construct TypeInfo<T> at runtime.")]
[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "TypeInfo<T> members are preserved when the type is known at compile time.")]
[UnconditionalSuppressMessage("Trimming", "IL2071", Justification = "TypeInfo<T> members are preserved when the type is known at compile time.")]
public static ITypeInfo GetTypeInfoDetails(this Type type)

Parameters

type Type

Type to get from.

Returns

ITypeInfo

The ITypeInfo.

GetTypeString(Type)

Get the type string for a Type.

public static string GetTypeString(this Type type)

Parameters

type Type

Type to get from.

Returns

string

A type string.

HasAttribute<T>(Type)

Check if a type has an attribute associated with it.

public static bool HasAttribute<T>(this Type type) where T : Attribute

Parameters

type Type

Type to check.

Returns

bool

True if there is an attribute, false if not.

Type Parameters

T

Attribute type to check for.

HasInterface(Type, Type)

Check if a type implements a specific interface.

[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "Reflection-based interface checks are used by compatibility fallback paths when generated metadata is unavailable.")]
public static bool HasInterface(this Type type, Type interfaceType)

Parameters

type Type

Type to check.

interfaceType Type

Interface to check for.

Returns

bool

True if the type implements the interface, false if not.

HasInterface<T>(Type)

Check if a type implements a specific interface.

public static bool HasInterface<T>(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if the type implements the interface, false if not.

Type Parameters

T

Interface to check for.

HasVisibleProperties(Type)

Indicates whether the Type has any public properties to get or set state.

[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "Reflection on public properties is used by compatibility paths; callers are responsible for preserving required types.")]
public static bool HasVisibleProperties(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if there are public properties (get or set), false otherwise.

Implements(Type, Type)

Check if a type implements another type - supporting interfaces, abstract types, with or without generics.

public static bool Implements(this Type type, Type super)

Parameters

type Type

Type to check.

super Type

Super / parent type to check against.

Returns

bool

True if derived, false if not.

ImplementsOpenGeneric(Type, Type)

Check if a type implements an open generic type.

[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "Reflection on interface implementations is used by compatibility paths.")]
public static bool ImplementsOpenGeneric(this Type type, Type openGenericType)

Parameters

type Type

Type to check.

openGenericType Type

Open generic Type to check for.

Returns

bool

True if type implements the open generic Type.

IsAPrimitiveType(Type)

Check if a type is a "primitive" type. This is not just dot net primitives but basic types like string, decimal, datetime, that are not classified as primitive types.

public static bool IsAPrimitiveType(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if Type is a primitive type.

IsBoolean(Type)

Check if a type is a Boolean or not.

public static bool IsBoolean(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is a boolean, false if not.

IsComparable(Type)

Check if a type is comparable, either implementing IComparable or IComparable<T>.

public static bool IsComparable(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is comparable, false if not an comparable.

IsDate(Type)

Check if a type is a Date or not.

public static bool IsDate(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is a date, false if not.

IsDateOnly(Type)

Check if a type is a DateOnly or not.

public static bool IsDateOnly(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is a date only, false if not.

IsDateTimeOffset(Type)

Check if a type is a DateTimeOffset or not.

public static bool IsDateTimeOffset(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is a date, false if not.

IsDelegate(Type)

Check whether or not a type is a delegate.

public static bool IsDelegate(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if delegate, false if not.

IsDerivedFromOpenGeneric(Type, Type)

Check if a type derives from an open generic type.

public static bool IsDerivedFromOpenGeneric(this Type type, Type openGenericType)

Parameters

type Type

Type to check.

openGenericType Type

Open generic Type to check for.

Returns

bool

True if type matches the open generic Type.

IsEnumerable(Type)

Check if a type is enumerable. Note that string is an IEnumerable, but in this case the string is excluded, as well as ExpandoObject.

public static bool IsEnumerable(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is enumerable, false if not an enumerable.

IsGuid(Type)

Check if a type is a Guid or not.

public static bool IsGuid(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is a Guid, false otherwise.

IsNullable(Type)

Check if a type is nullable or not.

public static bool IsNullable(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is nullable, false if not.

IsNullableContext(Type)

Check whether or not a type is in a nullable context.

public static bool IsNullableContext(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if it is in a nullable context, false if not.

IsNullableReferenceType(ICustomAttributeProvider)

Check whether or not a member that implements ICustomAttributeProvider is a nullable reference type.

public static bool IsNullableReferenceType(this ICustomAttributeProvider member)

Parameters

member ICustomAttributeProvider

Member that implements ICustomAttributeProvider to check.

Returns

bool

True if it nullable, false if not.

IsNumericType(Type)

Check if a type is a number or not.

public static bool IsNumericType(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is numeric, false if not.

IsRecord(Type)

Check whether or not a type is a record type.

[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "Reflection-based record checks are used by compatibility fallback paths when generated metadata is unavailable.")]
public static bool IsRecord(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if record, false if not.

IsString(Type)

Check if a type is a String or not.

public static bool IsString(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is a string, false otherwise.

IsTimeOnly(Type)

Check if a type is a TimeOnly or not.

public static bool IsTimeOnly(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is a time only, false if not.

IsTimeSpan(Type)

Check if a type is a TimeSpan or not.

public static bool IsTimeSpan(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is a time span, false if not.

Is<T>(Type)

Check if the type is of the type specified in the generic param.

public static bool Is<T>(this Type type)

Parameters

type Type

Type to check.

Returns

bool

True if type is a date, false if not.

Type Parameters

T

Type of the instance.