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
ciConstructorInfoThe constructor to wrap.
Returns
Exceptions
- InvalidOperationException
When
cicontains 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
ciConstructorInfoThe constructor to wrap.
Returns
- Func<T>
A delegate that calls
cithrough reflection, each optional parameter at its default, and casts the result (an InvalidCastException when it is not aT).
Type Parameters
TThe time of
ci
Exceptions
- InvalidOperationException
When
cicontains 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
methodInfoMethodInfoThe method.
targetobjectThe 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
refparameter). 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
valueEnumThe enum value.
Returns
- T
The attribute, or null when the member has none.
Type Parameters
TThe 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
typeTypeThe type whose constructor to find.
Returns
- ConstructorInfo
The constructor, or null.
Exceptions
- ArgumentNullException
When
typeis 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
TTypeThe 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
typeTypeThe 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
typeis 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
sourceTThe enumeration value.
Returns
- string
The string value stored in the value's description attribute.
Type Parameters
TThe 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
tupleTypeTypeThe tuple type.
Returns
- IEnumerable<Type>
The item types, read lazily by reflection.
Exceptions
- NullReferenceException
When
tupleTypeis null.
GetValueTupleItemTypes(Type)
The item types of a tuple type, in order, as a list.
public static List<Type> GetValueTupleItemTypes(this Type tupleType)
Parameters
tupleTypeTypeThe tuple type.
Returns
HasInterface(object, Type)
Checks if the type/object has this interface.
public static bool HasInterface(this object obj, Type @interface)
Parameters
Returns
- bool
Whether
obj's run-time type is assignable tointerface.
HasInterface(Type, Type)
Checks if the type/object has this interface.
public static bool HasInterface(this Type type, Type @interface)
Parameters
Returns
- bool
Whether
typeis assignable tointerface.
HasInterface<T>(object)
Checks if the type/object has this interface.
public static bool HasInterface<T>(this object obj)
Parameters
Returns
- bool
Whether
obj's run-time type is assignable toT(base classes count too).
Type Parameters
TThe interface, or any type
objmay derive from.
HasInterface<T>(Type)
Checks if the type/object has this interface.
public static bool HasInterface<T>(this Type type)
Parameters
Returns
- bool
Whether
typeis assignable toT(base classes count too).
Type Parameters
TThe interface, or any type
typemay derive from.
IsSameOrInherits(Type, Type)
Whether actualType is expectedType or assignable to it.
public static bool IsSameOrInherits(this Type actualType, Type expectedType)
Parameters
Returns
Exceptions
- NullReferenceException
When the types differ and
expectedTypeis 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
genericTypeThe open generic type, e.g.
typeof(List<>).toCheckTypeThe type whose base classes to walk; interfaces are not checked.
Returns
- bool
Whether
toCheckor one of its base classes is a construction ofgeneric.
IsValueTuple(object)
Whether obj is a tuple; see IsValueTupleType(Type) for what counts.
public static bool IsValueTuple(this object obj)
Parameters
objobjectThe object to test.
Returns
Exceptions
- NullReferenceException
When
objis 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
typeTypeThe type to test.
Returns
- bool
Whether it is a ValueTuple of any arity or a Tuple.
Exceptions
- NullReferenceException
When
typeis 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
tupleTypeobjectThe tuple (despite the parameter's name, the value, not its type).
Returns
- IEnumerable<object>
The items, read lazily by reflection.
Exceptions
- NullReferenceException
When
tupleTypeis null.