PeanutButter
Loading...
Searching...
No Matches
PeanutButter.Utils.TypeExtensions Class Reference

Helper extensions for Types. More...

Static Public Member Functions

static Type[] Ancestry (this Type type)
 Enumerates the ancestry of a Type.
static Type[] AncestryUntil (this Type type, Type from)
 Enumerates the ancestry of a Type, from the given type.
static IEnumerable< Type > WalkAncestry (this Type type, Type oldest)
 Walks the type ancestry of a type, starting at that type. If you'd like the ancestry the other way around, look at Ancestry and AncestryUntil. This variant is also lazy, so bailing out stops the walk.
static Dictionary< string, object > GetAllConstants (this Type type)
 Returns a dictionary of all constant values defined on a Type.
static Dictionary< string, T > GetAllConstants< T > (this Type type)
 Returns a dictionary of all constant values of a specified Type found on a Type.
static IEnumerable< object > GetAllConstantValues (this Type type)
 Returns a collection of all the constant values defined on a Type.
static IEnumerable< T > GetAllConstantValues< T > (this Type type)
 Returns a collection of all the constant values defined on a Type, restricted to the required Type T.
static bool HasDefaultConstructor (this Type type)
 Tests if a Type has a default constructor (ie, a constructor with no parameters)
static bool IsArrayOrAssignableFromArray (this Type t)
 Tests if a type is an array or could be assigned from an array.
static bool IsEnum (this Type t)
 Provides an extension method mimicking the full framework IsEnum for a single point of code usage.
static Assembly GetAssembly (this Type t)
 Provides an extension method mimicking the full framework GetAssembly for a single point of code usage.
static Type BaseType (this Type type)
 Provides an extension method mimicking the full framework BaseType for a single point of code usage.
static bool IsGenericType (this Type t)
 Provides an extension method mimicking the full framework IsGenericType for a single point of code usage.
static bool IsGenericOf (this Type t, Type genericTest)
 Tests if a type is a generic of a given generic type (eg typeof(List<>))
static bool IsAssignableFromArrayOf< T > (this Type t)
 Tests if a type is assignable from an array of T.
static bool ImplementsEnumerableGenericType (this Type t)
 Tests if a type implements IEnumerable<>.
static bool ImplementsIDictionaryGenericType (this Type type)
 Returns true if the provided type implements IDictionary<,> anywhere in the type heirachy.
static bool IsIDictionary (this Type type)
 Returns true if a type directly implements IDictionary<,>.
static bool TryGetDictionaryKeyAndValueTypes (this Type type, out Type keyType, out Type valueType)
 Tries to get the key and value types for an object, if it implements IDictionary<TKey,TValue>. Returns true if sucessful (with the out parameters set) or false if the provided type does not implement IDictionary<,>.
static Type TryGetEnumerableInterface (this Type srcType)
 Attempts to get the implemented Generic IEnumerable interface for a type, if possible.
static Type TryGetEnumerableItemType (this Type srcType)
 Attempts to get the item type (T) for a Type which is assumed to implement IEnumerable<T>.
static bool IsGenericOfIEnumerable (this Type arg)
 Tests if a type directly implements the generic IEnumerable interface.
static bool IsEnumerable (this Type arg)
 Tests if a type is enumerable by the duck-typing that .net understands (ie, could wrap with EnumerableWrapper)
static bool HasMethod (this Type t, string methodName)
 Tests if a type has a named method, without testing return type or parameters.
static bool HasVoidMethod (this Type t, string methodName, params Type[] parameters)
 Test if a type has a method returning void, with the optional list of parameter types.
static bool HasMethod (this Type t, string name, Type returnType, params Type[] parameterTypes)
 Tests if a type has the named method with the provided return type and optional parameter types.
static Type GetCollectionItemType (this Type collectionType)
 Attempts to get the item type of a collection.
static Type[] GetAllImplementedInterfaces (this Type inspectType)
 Attempts to find all implemented interfaces (and inherited ones) for a Type.
static bool IsDisposable (this Type t)
 Tests if a type implements IDisposable.
static string PrettyName (this Type type)
 Provides a "pretty" name for a type, taking into account generics and nullable types.
static bool IsCollection (this Type type)
 Rudimentary test for if a type is a collection type, testing for IEnumerable<> interface implementation as well as some baked-in known generic types.
static bool CanBeAssignedNull (this Type type)
 Determines if an object of this type can be assigned null.
static bool IsValueType (this Type type)
 Provides single method to determine IsValueType (shimmed for NETSTANDARD)
static bool IsAssignableOrUpCastableTo (this Type src, Type target)
 Returns true if the type being operated on can be directly assigned or implicitly upcast to the target type.
static bool CanImplicitlyCastTo (this Type source, Type target)
 Returns true if the type being operated on can be implicitly upcast to the target type.
static bool TryImplicitlyCastTo (this object srcValue, Type targetType, out object castValue)
 Returns true if the type being operated on can be implicitly upcast to the target type (value types only, so far)
static object DefaultValue (this Type type)
 Returns the default value for the type being operated on.
static bool IsInterface (this Type type)
 Cross-target shim for the IsInterface property, found on Type in NetFramework and on Type.GetTypeInf() on NETSTANDARD.
static bool IsNumericType (this Type type)
 Determines whether the provided type is a known numeric type (ie int / short / byte / double / float / decimal )
static bool IsAncestorOf (this Type type, Type test)
 Determines whether the type being operated on is an ancestor of the other type ie typeof(object).IsAncestorOf(typeof(Foo)) => true.
static bool Inherits (this Type type, Type test)
 Returns true when the type being operated on inherits from the test type, ie typeof(Foo).Inherits(typeof(object)) => true.
static bool Implements< T > (this Type type)
 Tests if the type being operated on implements the provided interfaceType.
static bool Implements (this Type type, Type interfaceType)
 Tests if the type being operated on implements the provided interfaceType.
static bool AllPublicInstancePropertiesAndMethodsAreVirtualOrAbstractAndNonFinal (this Type type)
 Tests if all public instance properties and methods are virtual or abstract and non-final (ie, can be mimicked with a duck)
static bool AllPublicInstancePropertiesAndMethodsAreNonFinal (this Type type)
 Tests if all public instance properties and methods are non-final.
static bool AllPublicInstanceMethodsAreNonFinal (this Type type)
 Tests if all public instance methods are non-final.
static bool AllPublicInstancePropertiesAreNonFinal (this Type type)
 Tests if all public instance properties are non-final.
static bool AllPublicInstancePropertiesAndMethodsAreVirtualOrAbstract (this Type type)
 Returns true if all public properties and methods are either virtual or abstract\ (ie can be properly overridden)
static bool AllPublicInstancePropertiesAreVirtualOrAbstract (this Type type)
 Returns true if all instance properties on the provided type are either virtual or abstract (ie, overridable)
static bool AllPublicInstanceMethodsAreVirtualOrAbstract (this Type type)
 Returns true if all instance methods on the provided type are either virtual or abstract (ie, overridable)
static T GetTopMostPropertyValue< T > (this object data, string propertyName)
 Retrieves the value of the "top-most" property in an inheritance hierarchy which matches the given name and type.
static void SetTopMostProperty< T > (this object data, string propertyName, T value)
 Sets the "top-most" property in an ancestry, useful for setting properties marked as "new" when you have access to the object cast to an ancestor type.
static bool IsVirtualOrAbstract (this MethodInfo methodInfo)
 returns true if the given method is virtual or abstract
static bool IsConcrete (this Type t)
 returns true if the type is not an interface or an abstract type
static bool IsVirtualOrAbstract (this PropertyInfo propertyInfo)
 returns true if the given property is virtual or abstract
static bool IsFinal (this PropertyInfo propertyInfo)
 returns true if the property is marked as IsFinal
static bool Implements< T > (this T obj, Type expected)
 returns true if the provided object implements the expected interface
static bool IsNullableType (this Type arg)
 Tests if the provided type is nullable.
static void SetStatic< T > (this Type t, string fieldOrPropertyName, T value)
 Attempts to set a static property or field value.
static T GetStatic< T > (this Type t, string fieldOrPropertyName)
 Attempts to get a static property or field value.
static bool IsPrimitiveOrImmutable (this Type type)
 Tests if the provided type is a "proper" primitive or some other built-in immutable type, eg DateTime, TimeSpan, Guid, DateTimeOffset.
static Type ResolveNullableUnderlyingType (this Type type)
 Resolves to the actual type or the underlying type T if the provided type is Nullable<T>.
static bool HasAttribute< TAttribute > (this Type type)
 Tests if a provided type or base type is decorated with the expected attribute [TAttribute].
static bool HasAttribute< TAttribute > (this Type type, Func< TAttribute, bool > matcher)
 Test if the provided type is decorated with [TAttribute], applying inheritance and the provided matcher.
static bool HasAttribute< TAttribute > (this Type type, bool inherit, Func< TAttribute, bool > matcher)
 Test if the provided type is decorated with [TAttribute], applying optional inheritance and the provided matcher.
static bool HasAttribute< TAttribute > (this Type type, bool inherit)
 Tests if a provided type is decorated with the attribute of type attributeType, including base types when enabled.
static bool HasAttribute (this Type type, Type attributeType, bool inherit)
 Tests if a provided type is decorated with the attribute of type attributeType, including base types when enabled.
static bool HasAttribute (this Type type, Type attributeType, bool inherit, Func< Attribute, bool > matcher)
 Tests if a provided type is decorated with the attribute of type attributeType, including base types when enabled.
static bool HasAttribute< TAttribute > (this MethodInfo methodInfo)
 Tests if the method (or any overridden base method) is decorated with [TAttribute].
static bool HasAttribute< TAttribute > (this MethodInfo methodInfo, Func< TAttribute, bool > matcher)
 Tests if the method (or any overridden base method) is decorated with [TAttribute].
static bool HasAttribute< TAttribute > (this MethodInfo methodInfo, bool inherit)
 Tests if the method is decorated with [TAttribute], including inheritance when enabled.
static bool HasAttribute< TAttribute > (this MethodInfo methodInfo, bool inherit, Func< TAttribute, bool > matcher)
 Tests if the method is decorated with [TAttribute], including inheritance when enabled.
static bool HasAttribute< TAttribute > (this ParameterInfo parameterInfo)
 Tests if the provided ParameterInfo is decorated with [TAttribute].
static bool HasAttribute< TAttribute > (this ParameterInfo parameterInfo, Func< TAttribute, bool > matcher)
 Tests if the provided ParameterInfo is decorated with [TAttribute], validating the attribute with the provided matcher.
static bool HasAttribute (this ParameterInfo parameterInfo, Type attributeType)
 Tests if the provided ParameterInfo has been decorated with an attribute of the given type, passing the provided matcher.
static bool HasAttribute (this ParameterInfo parameterInfo, Type attributeType, Func< Attribute, bool > matcher)
 Tests if the provided ParameterInfo has been decorated with an attribute of the given type, passing the provided matcher.
static bool HasAttribute (this MethodInfo methodInfo, Type attributeType, bool inherit)
 Tests if a provided method is decorated with the attribute of type attributeType, including inheritance when enabled.
static bool HasAttribute (this MethodInfo methodInfo, Type attributeType, bool inherit, Func< Attribute, bool > matcher)
 Tests if a provided method is decorated with the attribute of type attributeType, including inheritance when enabled.
static bool HasAttribute< TAttribute > (this PropertyInfo propertyInfo)
 Tests if the property (or base property that is overridden) is decorated with [TAttribute].
static bool HasAttribute< TAttribute > (this PropertyInfo propertyInfo, bool inherit)
 Tests if the property is decorated with [TAttribute], directly specifying whether or not to inherit attributes from base members.
static bool HasAttribute< TAttribute > (this PropertyInfo propertyInfo, bool inherit, Func< TAttribute, bool > matcher)
 Tests if the property is decorated with [TAttribute], directly specifying whether or not to inherit attributes from base members.
static bool HasAttribute (this PropertyInfo propertyInfo, Type attributeType, bool inherit)
 Tests if the property is decorated with [TAttribute].
static bool HasAttribute (this PropertyInfo propertyInfo, Type attributeType, bool inherit, Func< Attribute, bool > matcher)
 Tests if the property is decorated with [TAttribute].
static bool IsCompilerGenerated (this Type type)
 Returns true if the type is compiler-generated This is always the case with anonymous types, but may also be the case with other types. Other heuristics for determining an anonymous type rely on naming (unreliable) and it being sealed.
static bool LooksAnonymous (this Type type)
 Takes a guess that the type is anonymous based on some rules:
static bool HasOnlyParameterlessConstructor (this Type type)
 Determine if a type has a single constructor and that constructor is parameterless.
static bool HasParameterlessConstructor (this Type type)
 Returns true if the type has a parameterless constructor Note that the type may have other constructors too.
static IEnumerable< AttributeWalkStepWalkAttributes (this Type type)
 Walks the attributes per-type on the ancestry of the provided type.

Detailed Description

Helper extensions for Types.

Member Function Documentation

◆ AllPublicInstanceMethodsAreNonFinal()

bool PeanutButter.Utils.TypeExtensions.AllPublicInstanceMethodsAreNonFinal ( this Type type)
static

Tests if all public instance methods are non-final.

Parameters
type
Returns

◆ AllPublicInstanceMethodsAreVirtualOrAbstract()

bool PeanutButter.Utils.TypeExtensions.AllPublicInstanceMethodsAreVirtualOrAbstract ( this Type type)
static

Returns true if all instance methods on the provided type are either virtual or abstract (ie, overridable)

Parameters
type
Returns

◆ AllPublicInstancePropertiesAndMethodsAreNonFinal()

bool PeanutButter.Utils.TypeExtensions.AllPublicInstancePropertiesAndMethodsAreNonFinal ( this Type type)
static

Tests if all public instance properties and methods are non-final.

Parameters
type
Returns

◆ AllPublicInstancePropertiesAndMethodsAreVirtualOrAbstract()

bool PeanutButter.Utils.TypeExtensions.AllPublicInstancePropertiesAndMethodsAreVirtualOrAbstract ( this Type type)
static

Returns true if all public properties and methods are either virtual or abstract\ (ie can be properly overridden)

Parameters
type
Returns

◆ AllPublicInstancePropertiesAndMethodsAreVirtualOrAbstractAndNonFinal()

bool PeanutButter.Utils.TypeExtensions.AllPublicInstancePropertiesAndMethodsAreVirtualOrAbstractAndNonFinal ( this Type type)
static

Tests if all public instance properties and methods are virtual or abstract and non-final (ie, can be mimicked with a duck)

Parameters
type
Returns

◆ AllPublicInstancePropertiesAreNonFinal()

bool PeanutButter.Utils.TypeExtensions.AllPublicInstancePropertiesAreNonFinal ( this Type type)
static

Tests if all public instance properties are non-final.

Parameters
type
Returns

◆ AllPublicInstancePropertiesAreVirtualOrAbstract()

bool PeanutButter.Utils.TypeExtensions.AllPublicInstancePropertiesAreVirtualOrAbstract ( this Type type)
static

Returns true if all instance properties on the provided type are either virtual or abstract (ie, overridable)

Parameters
type
Returns

◆ Ancestry()

Type[] PeanutButter.Utils.TypeExtensions.Ancestry ( this Type type)
static

Enumerates the ancestry of a Type.

Parameters
typeStarting Type
Returns
The Type ancestry, starting from Object

◆ AncestryUntil()

Type[] PeanutButter.Utils.TypeExtensions.AncestryUntil ( this Type type,
Type from )
static

Enumerates the ancestry of a Type, from the given type.

  • if the given type is not found in the ancestry, the entire ancestry will be returned
  • you may provide a generic type without parameters, eg GenericBuilder<,> in which case the search is from the first occurence of that generic base type within the ancestry tree
Parameters
typeType to operate on (final type in the result)
fromType to truncate history at (first type in the result, when found)
Returns

◆ BaseType()

Type PeanutButter.Utils.TypeExtensions.BaseType ( this Type type)
static

Provides an extension method mimicking the full framework BaseType for a single point of code usage.

Parameters
type
Returns

◆ CanBeAssignedNull()

bool PeanutButter.Utils.TypeExtensions.CanBeAssignedNull ( this Type type)
static

Determines if an object of this type can be assigned null.

Parameters
type
Returns

◆ CanImplicitlyCastTo()

bool PeanutButter.Utils.TypeExtensions.CanImplicitlyCastTo ( this Type source,
Type target )
static

Returns true if the type being operated on can be implicitly upcast to the target type.

Parameters
source
target
Returns

◆ DefaultValue()

object PeanutButter.Utils.TypeExtensions.DefaultValue ( this Type type)
static

Returns the default value for the type being operated on.

Parameters
type
Returns

◆ GetAllConstants()

Dictionary< string, object > PeanutButter.Utils.TypeExtensions.GetAllConstants ( this Type type)
static

Returns a dictionary of all constant values defined on a Type.

Parameters
typeSource type to search for constants
Returns
Dictionary of constants, keyed by constant name

◆ GetAllConstants< T >()

Dictionary< string, T > PeanutButter.Utils.TypeExtensions.GetAllConstants< T > ( this Type type)
static

Returns a dictionary of all constant values of a specified Type found on a Type.

Parameters
typeType to search for constants
Template Parameters
TOnly return constants of this Type
Returns
Dictionary of all constant values on a specified type

◆ GetAllConstantValues()

IEnumerable< object > PeanutButter.Utils.TypeExtensions.GetAllConstantValues ( this Type type)
static

Returns a collection of all the constant values defined on a Type.

Parameters
typeType to search for constants
Returns
Collection of the constant values without their defined names

◆ GetAllConstantValues< T >()

IEnumerable< T > PeanutButter.Utils.TypeExtensions.GetAllConstantValues< T > ( this Type type)
static

Returns a collection of all the constant values defined on a Type, restricted to the required Type T.

Parameters
typeType to search for constants
Template Parameters
TOnly return constants of this Type
Returns
Collection of constant values from the source type which match the Type T restriction

◆ GetAllImplementedInterfaces()

Type[] PeanutButter.Utils.TypeExtensions.GetAllImplementedInterfaces ( this Type inspectType)
static

Attempts to find all implemented interfaces (and inherited ones) for a Type.

Parameters
inspectTypeType to inspect
Returns
Array of all interfaces which are implemented

◆ GetAssembly()

Assembly PeanutButter.Utils.TypeExtensions.GetAssembly ( this Type t)
static

Provides an extension method mimicking the full framework GetAssembly for a single point of code usage.

Parameters
t
Returns

◆ GetCollectionItemType()

Type PeanutButter.Utils.TypeExtensions.GetCollectionItemType ( this Type collectionType)
static

Attempts to get the item type of a collection.

Parameters
collectionTypeType to inspect
Returns
Item type, if it can be found, or null

◆ GetStatic< T >()

T PeanutButter.Utils.TypeExtensions.GetStatic< T > ( this Type t,
string fieldOrPropertyName )
static

Attempts to get a static property or field value.

Parameters
t
fieldOrPropertyName
Template Parameters
T

◆ GetTopMostPropertyValue< T >()

T PeanutButter.Utils.TypeExtensions.GetTopMostPropertyValue< T > ( this object data,
string propertyName )
static

Retrieves the value of the "top-most" property in an inheritance hierarchy which matches the given name and type.

Parameters
data
propertyName
Template Parameters
T
Returns
Exceptions
InvalidOperationException

◆ HasAttribute() [1/8]

bool PeanutButter.Utils.TypeExtensions.HasAttribute ( this MethodInfo methodInfo,
Type attributeType,
bool inherit )
static

Tests if a provided method is decorated with the attribute of type attributeType, including inheritance when enabled.

Parameters
methodInfo
attributeType
inherit
Returns

◆ HasAttribute() [2/8]

bool PeanutButter.Utils.TypeExtensions.HasAttribute ( this MethodInfo methodInfo,
Type attributeType,
bool inherit,
Func< Attribute, bool > matcher )
static

Tests if a provided method is decorated with the attribute of type attributeType, including inheritance when enabled.

Parameters
methodInfo
attributeType
inherit
matcher
Returns

◆ HasAttribute() [3/8]

bool PeanutButter.Utils.TypeExtensions.HasAttribute ( this ParameterInfo parameterInfo,
Type attributeType )
static

Tests if the provided ParameterInfo has been decorated with an attribute of the given type, passing the provided matcher.

Parameters
parameterInfo
attributeType
Returns

◆ HasAttribute() [4/8]

bool PeanutButter.Utils.TypeExtensions.HasAttribute ( this ParameterInfo parameterInfo,
Type attributeType,
Func< Attribute, bool > matcher )
static

Tests if the provided ParameterInfo has been decorated with an attribute of the given type, passing the provided matcher.

Parameters
parameterInfo
attributeType
matcher
Returns

◆ HasAttribute() [5/8]

bool PeanutButter.Utils.TypeExtensions.HasAttribute ( this PropertyInfo propertyInfo,
Type attributeType,
bool inherit )
static

Tests if the property is decorated with [TAttribute].

Parameters
propertyInfo
attributeType
inherit
Returns

◆ HasAttribute() [6/8]

bool PeanutButter.Utils.TypeExtensions.HasAttribute ( this PropertyInfo propertyInfo,
Type attributeType,
bool inherit,
Func< Attribute, bool > matcher )
static

Tests if the property is decorated with [TAttribute].

Parameters
propertyInfo
attributeType
inherit
matcher
Returns

◆ HasAttribute() [7/8]

bool PeanutButter.Utils.TypeExtensions.HasAttribute ( this Type type,
Type attributeType,
bool inherit )
static

Tests if a provided type is decorated with the attribute of type attributeType, including base types when enabled.

Parameters
type
attributeType
inherit
Returns

◆ HasAttribute() [8/8]

bool PeanutButter.Utils.TypeExtensions.HasAttribute ( this Type type,
Type attributeType,
bool inherit,
Func< Attribute, bool > matcher )
static

Tests if a provided type is decorated with the attribute of type attributeType, including base types when enabled.

Parameters
type
attributeType
inherit
matcher
Returns

◆ HasAttribute< TAttribute >() [1/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this MethodInfo methodInfo)
static

Tests if the method (or any overridden base method) is decorated with [TAttribute].

Parameters
methodInfo
Template Parameters
TAttribute
Returns
Type Constraints
TAttribute :Attribute 

◆ HasAttribute< TAttribute >() [2/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this MethodInfo methodInfo,
bool inherit )
static

Tests if the method is decorated with [TAttribute], including inheritance when enabled.

Parameters
methodInfo
inherit
Template Parameters
TAttribute
Returns
Type Constraints
TAttribute :Attribute 

◆ HasAttribute< TAttribute >() [3/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this MethodInfo methodInfo,
bool inherit,
Func< TAttribute, bool > matcher )
static

Tests if the method is decorated with [TAttribute], including inheritance when enabled.

Parameters
methodInfo
inherit
matcher
Template Parameters
TAttribute
Returns
Type Constraints
TAttribute :Attribute 

◆ HasAttribute< TAttribute >() [4/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this MethodInfo methodInfo,
Func< TAttribute, bool > matcher )
static

Tests if the method (or any overridden base method) is decorated with [TAttribute].

Parameters
methodInfo
matcher
Template Parameters
TAttribute
Returns
Type Constraints
TAttribute :Attribute 

◆ HasAttribute< TAttribute >() [5/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this ParameterInfo parameterInfo)
static

Tests if the provided ParameterInfo is decorated with [TAttribute].

Parameters
parameterInfo
Template Parameters
TAttribute
Returns
Type Constraints
TAttribute :Attribute 

◆ HasAttribute< TAttribute >() [6/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this ParameterInfo parameterInfo,
Func< TAttribute, bool > matcher )
static

Tests if the provided ParameterInfo is decorated with [TAttribute], validating the attribute with the provided matcher.

Parameters
parameterInfo
matcher
Template Parameters
TAttribute
Returns
Type Constraints
TAttribute :Attribute 

◆ HasAttribute< TAttribute >() [7/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this PropertyInfo propertyInfo)
static

Tests if the property (or base property that is overridden) is decorated with [TAttribute].

Parameters
propertyInfo
Template Parameters
TAttribute
Returns
Type Constraints
TAttribute :Attribute 

◆ HasAttribute< TAttribute >() [8/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this PropertyInfo propertyInfo,
bool inherit )
static

Tests if the property is decorated with [TAttribute], directly specifying whether or not to inherit attributes from base members.

Parameters
propertyInfo
inherit
Template Parameters
TAttribute
Returns
Type Constraints
TAttribute :Attribute 

◆ HasAttribute< TAttribute >() [9/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this PropertyInfo propertyInfo,
bool inherit,
Func< TAttribute, bool > matcher )
static

Tests if the property is decorated with [TAttribute], directly specifying whether or not to inherit attributes from base members.

Parameters
propertyInfo
inherit
matcher
Template Parameters
TAttribute
Returns
Type Constraints
TAttribute :Attribute 

◆ HasAttribute< TAttribute >() [10/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this Type type)
static

Tests if a provided type or base type is decorated with the expected attribute [TAttribute].

Parameters
type
Template Parameters
TAttribute
Returns
Type Constraints
TAttribute :Attribute 

◆ HasAttribute< TAttribute >() [11/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this Type type,
bool inherit )
static

Tests if a provided type is decorated with the attribute of type attributeType, including base types when enabled.

Parameters
type
inherit
Template Parameters
TAttribute
Returns

◆ HasAttribute< TAttribute >() [12/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this Type type,
bool inherit,
Func< TAttribute, bool > matcher )
static

Test if the provided type is decorated with [TAttribute], applying optional inheritance and the provided matcher.

Parameters
type
inherit
matcher
Template Parameters
TAttribute
Returns

◆ HasAttribute< TAttribute >() [13/13]

bool PeanutButter.Utils.TypeExtensions.HasAttribute< TAttribute > ( this Type type,
Func< TAttribute, bool > matcher )
static

Test if the provided type is decorated with [TAttribute], applying inheritance and the provided matcher.

Parameters
type
matcher
Template Parameters
TAttribute
Returns

◆ HasDefaultConstructor()

bool PeanutButter.Utils.TypeExtensions.HasDefaultConstructor ( this Type type)
static

Tests if a Type has a default constructor (ie, a constructor with no parameters)

Parameters
typeType to inspect
Returns
True when the type has a parameterless constructor; False otherwise. Note that a constructor with parameters which have all default values is not considered valid.

◆ HasMethod() [1/2]

bool PeanutButter.Utils.TypeExtensions.HasMethod ( this Type t,
string methodName )
static

Tests if a type has a named method, without testing return type or parameters.

Parameters
t
methodName
Returns

◆ HasMethod() [2/2]

bool PeanutButter.Utils.TypeExtensions.HasMethod ( this Type t,
string name,
Type returnType,
params Type[] parameterTypes )
static

Tests if a type has the named method with the provided return type and optional parameter types.

Parameters
t
name
returnType
parameterTypes
Returns

◆ HasOnlyParameterlessConstructor()

bool PeanutButter.Utils.TypeExtensions.HasOnlyParameterlessConstructor ( this Type type)
static

Determine if a type has a single constructor and that constructor is parameterless.

Parameters
type
Returns

◆ HasParameterlessConstructor()

bool PeanutButter.Utils.TypeExtensions.HasParameterlessConstructor ( this Type type)
static

Returns true if the type has a parameterless constructor Note that the type may have other constructors too.

Parameters
type
Returns

◆ HasVoidMethod()

bool PeanutButter.Utils.TypeExtensions.HasVoidMethod ( this Type t,
string methodName,
params Type[] parameters )
static

Test if a type has a method returning void, with the optional list of parameter types.

Parameters
t
methodName
parameters
Returns

◆ Implements()

bool PeanutButter.Utils.TypeExtensions.Implements ( this Type type,
Type interfaceType )
static

Tests if the type being operated on implements the provided interfaceType.

Parameters
type
interfaceType
Returns

◆ Implements< T >() [1/2]

bool PeanutButter.Utils.TypeExtensions.Implements< T > ( this T obj,
Type expected )
static

returns true if the provided object implements the expected interface

Parameters
obj
expected
Template Parameters
T
Returns
Exceptions
InvalidOperationException

◆ Implements< T >() [2/2]

bool PeanutButter.Utils.TypeExtensions.Implements< T > ( this Type type)
static

Tests if the type being operated on implements the provided interfaceType.

Parameters
type
Template Parameters
T
Returns
Type Constraints
T :class 

◆ ImplementsEnumerableGenericType()

bool PeanutButter.Utils.TypeExtensions.ImplementsEnumerableGenericType ( this Type t)
static

Tests if a type implements IEnumerable<>.

Parameters
tType to test
Returns
True if the source type implements IEnumerable<>; False otherwise

◆ ImplementsIDictionaryGenericType()

bool PeanutButter.Utils.TypeExtensions.ImplementsIDictionaryGenericType ( this Type type)
static

Returns true if the provided type implements IDictionary<,> anywhere in the type heirachy.

Parameters
type
Returns

◆ Inherits()

bool PeanutButter.Utils.TypeExtensions.Inherits ( this Type type,
Type test )
static

Returns true when the type being operated on inherits from the test type, ie typeof(Foo).Inherits(typeof(object)) => true.

Parameters
type
test
Returns

◆ IsAncestorOf()

bool PeanutButter.Utils.TypeExtensions.IsAncestorOf ( this Type type,
Type test )
static

Determines whether the type being operated on is an ancestor of the other type ie typeof(object).IsAncestorOf(typeof(Foo)) => true.

Parameters
type
test
Returns

◆ IsArrayOrAssignableFromArray()

bool PeanutButter.Utils.TypeExtensions.IsArrayOrAssignableFromArray ( this Type t)
static

Tests if a type is an array or could be assigned from an array.

Parameters
tType to check
Returns
True if {t} is an Array type or could have an array type assigned to it; False otherwise

◆ IsAssignableFromArrayOf< T >()

bool PeanutButter.Utils.TypeExtensions.IsAssignableFromArrayOf< T > ( this Type t)
static

Tests if a type is assignable from an array of T.

Parameters
tType to test
Template Parameters
TItem type of array which calling code would like to assign
Returns
True if the parameter type is assignable from an array of T

◆ IsAssignableOrUpCastableTo()

bool PeanutButter.Utils.TypeExtensions.IsAssignableOrUpCastableTo ( this Type src,
Type target )
static

Returns true if the type being operated on can be directly assigned or implicitly upcast to the target type.

Parameters
src
target
Returns

◆ IsCollection()

bool PeanutButter.Utils.TypeExtensions.IsCollection ( this Type type)
static

Rudimentary test for if a type is a collection type, testing for IEnumerable<> interface implementation as well as some baked-in known generic types.

Parameters
type
Returns

◆ IsCompilerGenerated()

bool PeanutButter.Utils.TypeExtensions.IsCompilerGenerated ( this Type type)
static

Returns true if the type is compiler-generated This is always the case with anonymous types, but may also be the case with other types. Other heuristics for determining an anonymous type rely on naming (unreliable) and it being sealed.

Parameters
type
Returns

◆ IsConcrete()

bool PeanutButter.Utils.TypeExtensions.IsConcrete ( this Type t)
static

returns true if the type is not an interface or an abstract type

Parameters
t
Returns

◆ IsDisposable()

bool PeanutButter.Utils.TypeExtensions.IsDisposable ( this Type t)
static

Tests if a type implements IDisposable.

Parameters
tType to test
Returns
True if it implements IDisposable; false otherwise

◆ IsEnum()

bool PeanutButter.Utils.TypeExtensions.IsEnum ( this Type t)
static

Provides an extension method mimicking the full framework IsEnum for a single point of code usage.

Parameters
t
Returns

◆ IsEnumerable()

bool PeanutButter.Utils.TypeExtensions.IsEnumerable ( this Type arg)
static

Tests if a type is enumerable by the duck-typing that .net understands (ie, could wrap with EnumerableWrapper)

Parameters
arg
Returns

◆ IsFinal()

bool PeanutButter.Utils.TypeExtensions.IsFinal ( this PropertyInfo propertyInfo)
static

returns true if the property is marked as IsFinal

Parameters
propertyInfo
Returns

◆ IsGenericOf()

bool PeanutButter.Utils.TypeExtensions.IsGenericOf ( this Type t,
Type genericTest )
static

Tests if a type is a generic of a given generic type (eg typeof(List<>))

Parameters
ttype to operate on
genericTesttype to test against (eg typeof(List<>))
Returns
True if the input type is a match, false otherwise

◆ IsGenericOfIEnumerable()

bool PeanutButter.Utils.TypeExtensions.IsGenericOfIEnumerable ( this Type arg)
static

Tests if a type directly implements the generic IEnumerable interface.

Parameters
argType to test
Returns
True if it does implement the generic IEnumerable; false otherwise

◆ IsGenericType()

bool PeanutButter.Utils.TypeExtensions.IsGenericType ( this Type t)
static

Provides an extension method mimicking the full framework IsGenericType for a single point of code usage.

Parameters
t
Returns

◆ IsIDictionary()

bool PeanutButter.Utils.TypeExtensions.IsIDictionary ( this Type type)
static

Returns true if a type directly implements IDictionary<,>.

Parameters
type
Returns

◆ IsInterface()

bool PeanutButter.Utils.TypeExtensions.IsInterface ( this Type type)
static

Cross-target shim for the IsInterface property, found on Type in NetFramework and on Type.GetTypeInf() on NETSTANDARD.

Parameters
type
Returns

◆ IsNullableType()

bool PeanutButter.Utils.TypeExtensions.IsNullableType ( this Type arg)
static

Tests if the provided type is nullable.

Parameters
arg
Returns

◆ IsNumericType()

bool PeanutButter.Utils.TypeExtensions.IsNumericType ( this Type type)
static

Determines whether the provided type is a known numeric type (ie int / short / byte / double / float / decimal )

Parameters
typeType to operate on
Returns

◆ IsPrimitiveOrImmutable()

bool PeanutButter.Utils.TypeExtensions.IsPrimitiveOrImmutable ( this Type type)
static

Tests if the provided type is a "proper" primitive or some other built-in immutable type, eg DateTime, TimeSpan, Guid, DateTimeOffset.

  • useful when performing recursive reflection - if you hit a type identified by this extension method, then you should probably stop recursing.
Parameters
type
Returns

◆ IsValueType()

bool PeanutButter.Utils.TypeExtensions.IsValueType ( this Type type)
static

Provides single method to determine IsValueType (shimmed for NETSTANDARD)

Parameters
type
Returns

◆ IsVirtualOrAbstract() [1/2]

bool PeanutButter.Utils.TypeExtensions.IsVirtualOrAbstract ( this MethodInfo methodInfo)
static

returns true if the given method is virtual or abstract

Parameters
methodInfo
Returns

◆ IsVirtualOrAbstract() [2/2]

bool PeanutButter.Utils.TypeExtensions.IsVirtualOrAbstract ( this PropertyInfo propertyInfo)
static

returns true if the given property is virtual or abstract

Parameters
propertyInfo
Returns

◆ LooksAnonymous()

bool PeanutButter.Utils.TypeExtensions.LooksAnonymous ( this Type type)
static

Takes a guess that the type is anonymous based on some rules:

  • class
  • sealed
  • non-public
  • null namespace
  • base type is object
  • compiler-generated
  • single parameterless constructor note that attempts to determine whether a type is anonymous by its name are all flawed, especially since there's no guarantee that the type was made in C# and F# allows all the characters in anonymous type names
Parameters
type
Returns

◆ PrettyName()

string PeanutButter.Utils.TypeExtensions.PrettyName ( this Type type)
static

Provides a "pretty" name for a type, taking into account generics and nullable types.

Parameters
type
Returns

◆ ResolveNullableUnderlyingType()

Type PeanutButter.Utils.TypeExtensions.ResolveNullableUnderlyingType ( this Type type)
static

Resolves to the actual type or the underlying type T if the provided type is Nullable<T>.

Parameters
type
Returns

◆ SetStatic< T >()

void PeanutButter.Utils.TypeExtensions.SetStatic< T > ( this Type t,
string fieldOrPropertyName,
T value )
static

Attempts to set a static property or field value.

Parameters
t
fieldOrPropertyName
value
Template Parameters
T

◆ SetTopMostProperty< T >()

void PeanutButter.Utils.TypeExtensions.SetTopMostProperty< T > ( this object data,
string propertyName,
T value )
static

Sets the "top-most" property in an ancestry, useful for setting properties marked as "new" when you have access to the object cast to an ancestor type.

Parameters
data
propertyName
value
Template Parameters
T

◆ TryGetDictionaryKeyAndValueTypes()

bool PeanutButter.Utils.TypeExtensions.TryGetDictionaryKeyAndValueTypes ( this Type type,
out Type keyType,
out Type valueType )
static

Tries to get the key and value types for an object, if it implements IDictionary<TKey,TValue>. Returns true if sucessful (with the out parameters set) or false if the provided type does not implement IDictionary<,>.

Parameters
type
keyType
valueType
Returns

◆ TryGetEnumerableInterface()

Type PeanutButter.Utils.TypeExtensions.TryGetEnumerableInterface ( this Type srcType)
static

Attempts to get the implemented Generic IEnumerable interface for a type, if possible.

Parameters
srcTypeType to search for the interface
Returns
Generic IEnumerable type implemented if found or null otherwise

◆ TryGetEnumerableItemType()

Type PeanutButter.Utils.TypeExtensions.TryGetEnumerableItemType ( this Type srcType)
static

Attempts to get the item type (T) for a Type which is assumed to implement IEnumerable<T>.

Parameters
srcTypeType to search for the IEnumerable <T> interface and underlying type
Returns
IEnumerable<> item type (T) implemented if found or null otherwise

◆ TryImplicitlyCastTo()

bool PeanutButter.Utils.TypeExtensions.TryImplicitlyCastTo ( this object srcValue,
Type targetType,
out object castValue )
static

Returns true if the type being operated on can be implicitly upcast to the target type (value types only, so far)

Parameters
targetType
srcValue
castValue
Returns

◆ WalkAncestry()

IEnumerable< Type > PeanutButter.Utils.TypeExtensions.WalkAncestry ( this Type type,
Type oldest )
static

Walks the type ancestry of a type, starting at that type. If you'd like the ancestry the other way around, look at Ancestry and AncestryUntil. This variant is also lazy, so bailing out stops the walk.

Parameters
type
oldest
Returns

◆ WalkAttributes()

IEnumerable< AttributeWalkStep > PeanutButter.Utils.TypeExtensions.WalkAttributes ( this Type type)
static

Walks the attributes per-type on the ancestry of the provided type.

Parameters
type
Returns
Exceptions
ArgumentNullException

The documentation for this class was generated from the following file:
  • source/Utils/PeanutButter.Utils/TypeExtensions.cs