Class TypeEx
- Namespace
- Utils.Reflection
- Assembly
- Utils.dll
Provides extension methods for various Type reflection operations.
public static class TypeEx
- Inheritance
-
TypeEx
- Inherited Members
Methods
GetGenericTypeDefinitionFor(Type, Type)
Returns the generic type arguments for the specified baseType that the current type is derived from.
public static Type[] GetGenericTypeDefinitionFor(this Type type, Type baseType)
Parameters
Returns
- Type[]
An array of generic type arguments if found; otherwise, null.
GetPropertiesOrFields(Type)
Gets all properties and fields of the specified type.
public static PropertyOrFieldInfo[] GetPropertiesOrFields(this Type type)
Parameters
typeTypeThe type whose properties and fields to retrieve.
Returns
- PropertyOrFieldInfo[]
An array of PropertyOrFieldInfo representing all properties and fields of the type.
GetPropertiesOrFields(Type, BindingFlags)
Gets all properties and fields of the specified type using the specified binding flags.
public static PropertyOrFieldInfo[] GetPropertiesOrFields(this Type type, BindingFlags bindingFlags)
Parameters
typeTypeThe type whose properties and fields to retrieve.
bindingFlagsBindingFlagsA combination of BindingFlags to control the search.
Returns
- PropertyOrFieldInfo[]
An array of PropertyOrFieldInfo representing all properties and fields of the type.
GetPropertyOrField(Type, string)
Gets a property or field by name from the specified type.
public static PropertyOrFieldInfo? GetPropertyOrField(this Type type, string name)
Parameters
typeTypeThe type from which to retrieve the property or field.
namestringThe name of the property or field.
Returns
- PropertyOrFieldInfo
A PropertyOrFieldInfo representing the found member, or null if no matching member exists.
Exceptions
- AmbiguousMatchException
Thrown when the lookup resolves to more than one member (for example, a property and a field with the same name in different scopes, or a hidden member in a derived class).
GetPropertyOrField(Type, string, BindingFlags)
Gets a property or field by name from the specified type, using the given binding flags.
public static PropertyOrFieldInfo? GetPropertyOrField(this Type type, string name, BindingFlags bindingFlags)
Parameters
typeTypeThe type from which to retrieve the property or field.
namestringThe name of the property or field.
bindingFlagsBindingFlagsA combination of BindingFlags to control the search.
Returns
- PropertyOrFieldInfo
A PropertyOrFieldInfo representing the found member, or null if no matching member exists.
Exceptions
- AmbiguousMatchException
Thrown when the lookup resolves to more than one member under the given
bindingFlags.
GetStaticMethod(Type, Type[], string, Type[])
Gets the public static method named methodName with the exact
argumentTypes from the closed generic type obtained by applying
genericTypeArguments to type.
public static MethodInfo? GetStaticMethod(this Type type, Type[] genericTypeArguments, string methodName, Type[] argumentTypes)
Parameters
typeTypeAn open generic type definition (e.g.
typeof(List<>)).genericTypeArgumentsType[]Type arguments to close the generic definition.
methodNamestringName of the static method to retrieve.
argumentTypesType[]Exact parameter types of the method to retrieve.
Returns
- MethodInfo
The matching MethodInfo, or null when no method with the given name and parameter types exists.
Exceptions
- ArgumentException
Thrown when
typeis not an open generic type definition, or when the number ofgenericTypeArgumentsdoes not match the type's arity.
GetStaticMethods(Type, Type[], string)
Gets all public static methods named methodName from the closed generic type
obtained by applying genericTypeArguments to type.
public static MethodInfo[] GetStaticMethods(this Type type, Type[] genericTypeArguments, string methodName)
Parameters
typeTypeAn open generic type definition (e.g.
typeof(List<>)).genericTypeArgumentsType[]Type arguments to close the generic definition.
methodNamestringName of the static methods to retrieve.
Returns
- MethodInfo[]
An array of matching MethodInfo instances; empty when none match.
Exceptions
- ArgumentException
Thrown when
typeis not an open generic type definition, or when the number ofgenericTypeArgumentsdoes not match the type's arity.
GetUnderlyingType(Type)
Gets the underlying type for the given type. If the type is an enum, it returns the enum's underlying type. If the type is nullable, it returns the underlying non-nullable type.
public static Type GetUnderlyingType(this Type type)
Parameters
typeTypeThe type whose underlying type to retrieve.
Returns
- Type
The underlying type, or the original type if it is not an enum or nullable.
IsAssignableFromEx(Type, Type)
Checks if toAssign can be implicitly assigned to a variable of type
toBeAssigned.
public static bool IsAssignableFromEx(this Type toBeAssigned, Type toAssign)
Parameters
Returns
- bool
true if a C# implicit conversion exists from
toAssigntotoBeAssigned, or if the CLR considerstoAssigndirectly assignable totoBeAssigned.
Remarks
For numeric types, only the implicit numeric conversions defined by the C# specification are
accepted. uint → int or long → int are therefore rejected even though they share
the same storage size. Use IsAssignableFrom(Type) when CLR assignability — rather
than C# implicit conversion — is the intended check.
IsDefinedBy(Type, Type)
Determines whether the specified <code class="paramref">type</code> is derived from, implements,
or is the same as the <code class="paramref">baseType</code>. This includes handling for generic
type definitions (e.g.
List<>
).
public static bool IsDefinedBy(this Type type, Type baseType)
Parameters
typeTypeThe concrete type to check.
baseTypeTypeThe base or interface type to check against. May be generic.
Returns
- bool
true if
typeis or inherits frombaseType, or implementsbaseType(if it's an interface), including handling for generic type definitions; otherwise, false.
Exceptions
- ArgumentNullException
Thrown if either
typeorbaseTypeisnull.