Table of Contents

Class ReflectionHelper

Namespace
Gym.Reflection
Assembly
Gym.dll

0.2's reflection extension methods on Type, ConstructorInfo, MethodInfo, enums and tuples. In 2.0 only DefaultConstructor<T> calls two of them (GetDefaultConstructor(Type) and CreateDefaultConstructor<T>(ConstructorInfo)), for the legacy deep merge.

[Obsolete("Part of the legacy Dict's merging: use the BCL's reflection (Type.GetConstructor(Type.EmptyTypes), Type.IsAssignableTo, System.Runtime.CompilerServices.ITuple). Removed in 2.0 with Gym.Legacy.")]
public static class ReflectionHelper
Inheritance
ReflectionHelper
Inherited Members

Remarks

Obsolete with the legacy Dict (DR-56) and removed in 2.0: the BCL covers each method (GetConstructor(Type[]) with EmptyTypes, IsAssignableFrom(Type), ITuple, CustomAttributeExtensions). Being extension methods, they appear on every object and Type in a file that imports Gym.Reflection.

Methods

CreateDefaultConstructor(ConstructorInfo)

Creates a Func<TResult> that returns ci's ParameterType to a constructor that all of it's parameters has default value.

public static Func<object> CreateDefaultConstructor(this ConstructorInfo ci)

Parameters

ci ConstructorInfo

The constructor to wrap.

Returns

Func<object>

A delegate that calls ci through reflection, each optional parameter at its default.

Exceptions

InvalidOperationException

When ci contains non-optional (IsOptional) parameters. Footgun: its message lists the optional parameters, not the offending ones.

CreateDefaultConstructor<T>(ConstructorInfo)

Creates a Func<TResult> that returns ci's ParameterType to a constructor that all of it's parameters has default value.

public static Func<T> CreateDefaultConstructor<T>(this ConstructorInfo ci)

Parameters

ci ConstructorInfo

The constructor to wrap.

Returns

Func<T>

A delegate that calls ci through reflection, each optional parameter at its default, and casts the result (an InvalidCastException when it is not a T).

Type Parameters

T

The time of ci

Exceptions

InvalidOperationException

When ci contains non-optional (IsOptional) parameters. Footgun: its message lists the optional parameters, not the offending ones.

CreateDelegate(MethodInfo, object)

Creates a delegate to a MethodInfo, typed Func<TResult>-like or Action-like from its signature.

public static Delegate CreateDelegate(this MethodInfo methodInfo, object target)

Parameters

methodInfo MethodInfo

The method.

target object

The instance an instance method binds to; ignored for a static method.

Returns

Delegate

The delegate.

Exceptions

ArgumentException

When the method cannot bind (for instance a ref parameter). Footgun: an instance method binds by name, so an overloaded name may bind the overload matching the delegate's signature rather than this one.

GetAttribute<T>(Enum)

The first T attribute on an enum value's member, or null. This extension method is broken out so you can use a similar pattern with other MetaData elements in the future. This is your base method for each.

public static T GetAttribute<T>(this Enum value) where T : Attribute

Parameters

value Enum

The enum value.

Returns

T

The attribute, or null when the member has none.

Type Parameters

T

The attribute type.

Exceptions

IndexOutOfRangeException

For a value that names no member (a combination of flags, or an undefined number).

GetDefaultConstructor(Type)

type's public parameterless constructor, or else its public constructor with the fewest parameters that are all optional; null when it has neither.

public static ConstructorInfo GetDefaultConstructor(this Type type)

Parameters

type Type

The type whose constructor to find.

Returns

ConstructorInfo

The constructor, or null.

Exceptions

ArgumentNullException

When type is null.

GetDefaultConstructor<TType>()

TType's parameterless constructor, or else its constructor with the fewest parameters that are all optional; null when it has neither.

public static ConstructorInfo GetDefaultConstructor<TType>()

Returns

ConstructorInfo

The constructor, or null.

Type Parameters

TType

The type whose constructor to find.

GetDefaultValue(Type)

Gets the default value for a type. This method should serve as a programmatic equivalent to default(T).

public static object GetDefaultValue(this Type type)

Parameters

type Type

The type whose default value should be retrieved.

Returns

object

A zeroed instance for a value type. Footgun: for a reference type it is not default(T)'s null but a new object from its default constructor.

Exceptions

NullReferenceException

For a reference type with no default constructor.

ArgumentNullException

When type is null.

GetDescription<T>(T)

Gets a the value of a DescriptionAttribute associated with a particular enumeration value.

public static string GetDescription<T>(this T source) where T : Enum

Parameters

source T

The enumeration value.

Returns

string

The string value stored in the value's description attribute.

Type Parameters

T

The enumeration type.

Exceptions

NullReferenceException

For a value that names no single member (a combination of flags, or an undefined number).

GetTuplePropertyTypes(Type)

A tuple type's item types, in order: a Tuple's property types, a value tuple's ItemN field types. Footgun: any type whose name starts with Tuple is read through its properties.

public static IEnumerable<Type> GetTuplePropertyTypes(this Type tupleType)

Parameters

tupleType Type

The tuple type.

Returns

IEnumerable<Type>

The item types, read lazily by reflection.

Exceptions

NullReferenceException

When tupleType is null.

GetValueTupleItemTypes(Type)

The item types of a tuple type, in order, as a list.

public static List<Type> GetValueTupleItemTypes(this Type tupleType)

Parameters

tupleType Type

The tuple type.

Returns

List<Type>

See GetTuplePropertyTypes(Type).

HasInterface(object, Type)

Checks if the type/object has this interface.

public static bool HasInterface(this object obj, Type @interface)

Parameters

obj object

The object to test; null gives false.

interface Type

The interface, or any type; null gives false.

Returns

bool

Whether obj's run-time type is assignable to interface.

HasInterface(Type, Type)

Checks if the type/object has this interface.

public static bool HasInterface(this Type type, Type @interface)

Parameters

type Type

The type to test; null gives false.

interface Type

The interface, or any type; null gives false.

Returns

bool

Whether type is assignable to interface.

HasInterface<T>(object)

Checks if the type/object has this interface.

public static bool HasInterface<T>(this object obj)

Parameters

obj object

The object to test; null gives false.

Returns

bool

Whether obj's run-time type is assignable to T (base classes count too).

Type Parameters

T

The interface, or any type obj may derive from.

HasInterface<T>(Type)

Checks if the type/object has this interface.

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

Parameters

type Type

The type to test; null gives false.

Returns

bool

Whether type is assignable to T (base classes count too).

Type Parameters

T

The interface, or any type type may derive from.

IsSameOrInherits(Type, Type)

Whether actualType is expectedType or assignable to it.

public static bool IsSameOrInherits(this Type actualType, Type expectedType)

Parameters

actualType Type

The type to test.

expectedType Type

The type it should be or derive from.

Returns

bool

true when the types are equal or actualType is assignable to expectedType.

Exceptions

NullReferenceException

When the types differ and expectedType is null.

IsSubclassOfRawGeneric(Type, Type)

Checks if type toCheck ihneriets type generic. Taken from: https://stackoverflow.com/a/457708/1481186

public static bool IsSubclassOfRawGeneric(Type generic, Type toCheck)

Parameters

generic Type

The open generic type, e.g. typeof(List<>).

toCheck Type

The type whose base classes to walk; interfaces are not checked.

Returns

bool

Whether toCheck or one of its base classes is a construction of generic.

IsValueTuple(object)

Whether obj is a tuple; see IsValueTupleType(Type) for what counts.

public static bool IsValueTuple(this object obj)

Parameters

obj object

The object to test.

Returns

bool

Whether its run-time type is a value tuple or a Tuple.

Exceptions

NullReferenceException

When obj is null.

IsValueTupleType(Type)

Whether type is a value tuple. Footgun: despite the name, reference tuples (Tuple, matched by name) count too.

public static bool IsValueTupleType(this Type type)

Parameters

type Type

The type to test.

Returns

bool

Whether it is a ValueTuple of any arity or a Tuple.

Exceptions

NullReferenceException

When type is null.

IterateTuple(object)

A tuple's items, in order: a Tuple's through its properties, a value tuple's through its Item1, Item2, ... fields. Footgun: an eight-or-more value tuple yields its first seven items only, since the rest sit in its Rest field.

public static IEnumerable<object> IterateTuple(this object tupleType)

Parameters

tupleType object

The tuple (despite the parameter's name, the value, not its type).

Returns

IEnumerable<object>

The items, read lazily by reflection.

Exceptions

NullReferenceException

When tupleType is null.