Class
- Namespace
- CoreFramework
Provides extension methods for reflection-related operations on types, methods, fields, and attributes.
public static class ReflectionExtensions
- Inheritance
-
object
Fields
PrimitiveTypeCastHierarchy
A hierarchical structure defining sets of primitive types that can be explicitly or implicitly cast between each other.
private static readonly Type[][] PrimitiveTypeCastHierarchy
Field Value
- Type[][]
Remarks
This two-dimensional array organizes related primitive types into groups, where each inner array represents a set of types that share compatible casting rules. The hierarchy is used for determining possible casts among primitive types for runtime reflection-based operations.
TypeDisplayNames
A dictionary mapping common .NET types to their user-friendly display names.
private static readonly Dictionary<Type, string> TypeDisplayNames
Field Value
- Dictionary<Type, string>
Remarks
This dictionary maps instances of System.Type representing common .NET types (e.g., int, string) to their simplified or more readable display names (e.g., “int”, “string”). It is primarily used in reflection-based operations to provide a human-readable type designation.
ValueTupleTypes
An array of System.Type objects representing various generic type definitions of System.ValueTuple.
private static readonly Type[] ValueTupleTypes
Field Value
- Type[]
Remarks
This array includes all generic forms of System.ValueTuple types with arity ranging from one to eight, such as System.ValueTuple<T1> through System.ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>. It is primarily used to identify System.ValueTuple types during reflection-based operations where type inspection or display name generation is required.
Methods
AreMethodsEqual(MethodInfo, MethodInfo)
Determines if two methods from different types have the same signature.
public static bool AreMethodsEqual(MethodInfo a, MethodInfo b)
Parameters
aMethodInfoFirst method
bMethodInfoSecond method
Returns
- bool
trueif they are equal
Cast(Type, object)
Dynamically casts an object to the specified type.
public static object Cast(this Type type, object data)
Parameters
typeTypeThe destination type of the cast.
dataobjectThe object to cast.
Returns
- object
The dynamically cast object.
Default(Type)
Returns the default value for the given type.
public static object Default(this Type type)
Parameters
typeTypeThe type for which to get the default value.
Returns
- object
An instance of the type with a default value, or null if the type is a reference type.
GetDisplayName(Type, bool)
Gets a formatted display name for a given type.
public static string GetDisplayName(this Type type, bool includeNamespace = false)
Parameters
typeTypeThe type to generate a display name for.
includeNamespaceboolIf the namespace should be included when generating the typename.
Returns
- string
The generated display name.
GetTupleDisplayName(Type, bool)
Gets a formatted display name for a tuple type.
private static string GetTupleDisplayName(this Type type, bool includeNamespace = false)
Parameters
typeTypeThe tuple type to generate a display name for.
includeNamespaceboolIf the namespace should be included when generating the typename.
Returns
- string
The generated display name for the tuple type.
HasAttribute<T>(ICustomAttributeProvider, bool)
Checks if the specified attribute is present on the provider.
public static bool HasAttribute<T>(this ICustomAttributeProvider provider, bool searchInherited = true) where T : Attribute
Parameters
providerICustomAttributeProviderThe attribute provider.
searchInheritedboolIf base declarations should be searched.
Returns
- bool
True if the attribute is present, otherwise false.
Type Parameters
TThe attribute to test.
HasCastDefined(Type, Type, bool)
Determines if a cast is defined between two types.
private static bool HasCastDefined(this Type from, Type to, bool implicitly)
Parameters
fromTypeThe source type to check for cast definitions.
toTypeThe destination type to check for cast definitions.
implicitlyboolIf only implicit casts should be considered.
Returns
- bool
True if a cast is defined between the types, otherwise false.
IsCastDefined(Type, Func<MethodInfo, Type>, Func<MethodInfo, Type>, bool, bool)
Determines if a cast is defined between two types.
private static bool IsCastDefined(Type type, Func<MethodInfo, Type> baseType, Func<MethodInfo, Type> derivedType, bool implicitly, bool lookInBase)
Parameters
typeTypeThe type to check for cast definitions.
baseTypeFunc<MethodInfo, Type>A function to get the base type from a method.
derivedTypeFunc<MethodInfo, Type>A function to get the derived type from a method.
implicitlyboolIf only implicit casts should be considered.
lookInBaseboolIf the base hierarchy should be searched for cast definitions.
Returns
- bool
True if a cast is defined between the types, otherwise false.
IsCastableTo(Type, Type, bool)
Determines if an object of the given type can be cast to the specified type.
public static bool IsCastableTo(this Type from, Type to, bool implicitly = false)
Parameters
fromTypethis object
toTypeThe destination type of the cast.
implicitlyboolIf only implicit casts should be considered.
Returns
- bool
If the cast can be performed.
IsDelegate(FieldInfo)
Determines if a field is a delegate.
public static bool IsDelegate(this FieldInfo fieldInfo)
Parameters
fieldInfoFieldInfoThe field to check.
Returns
- bool
True if the field is a delegate, otherwise false.
IsDelegate(Type)
Determines if a type is a delegate.
public static bool IsDelegate(this Type type)
Parameters
typeTypeThe type to check.
Returns
- bool
True if the type is a delegate, otherwise false.
IsDerivedTypeOf(Type, Type)
Determines if the type is a derived type of the given base type.
public static bool IsDerivedTypeOf(this Type type, Type baseType)
Parameters
typeTypethis type
baseTypeTypeThe base type to test against.
Returns
- bool
If the type is a derived type of the base type.
IsGenericTypeOf(Type, Type)
Determines if the type is a generic type of the given non-generic type.
public static bool IsGenericTypeOf(this Type genericType, Type nonGenericType)
Parameters
genericTypeTypeThe Type to be used
nonGenericTypeTypeThe non-generic type to test against.
Returns
- bool
If the type is a generic type of the non-generic type.
IsOverride(MethodInfo)
Determines if the given method is an override.
public static bool IsOverride(this MethodInfo methodInfo)
Parameters
methodInfoMethodInfoThe method to check.
Returns
- bool
True if the method is an override, otherwise false.
IsStrongDelegate(FieldInfo)
Determines if a field is a strongly typed delegate.
public static bool IsStrongDelegate(this FieldInfo fieldInfo)
Parameters
fieldInfoFieldInfoThe field to query.
Returns
- bool
True if the field is a strongly typed delegate, otherwise false.
IsStrongDelegate(Type)
Determines if a type is a strongly typed delegate.
public static bool IsStrongDelegate(this Type type)
Parameters
typeTypeThe type to check.
Returns
- bool
True if the type is a strongly typed delegate, otherwise false.
Is<T>(Type)
Determines if a type is assignable from the specified generic type parameter.
public static bool Is<T>(this Type type)
Parameters
typeTypeThe type to check.
Returns
- bool
True if the specified type is assignable from the generic type parameter T, otherwise false.
Type Parameters
TThe type to check against.
RebaseMethod(MethodInfo, Type)
Rebase a method onto a new type by finding the corresponding method with an equal signature.
public static MethodInfo RebaseMethod(this MethodInfo method, Type newBase)
Parameters
methodMethodInfoMethod to rebase
newBaseTypeNew type to rebase the method onto
Returns
- MethodInfo
The rebased method