Class ReflectionHelper
- Namespace
- Ecng.Reflection
- Assembly
- Ecng.Reflection.dll
Provides helper methods for reflection.
public static class ReflectionHelper
- Inheritance
-
ReflectionHelper
- Inherited Members
Fields
AddPrefix
Adders prefix.
public const string AddPrefix = "add_"
Field Value
AllInstanceMembers
Binding flags for all instance members.
public const BindingFlags AllInstanceMembers = Instance | Public | NonPublic
Field Value
AllMembers
Binding flags for all members.
public const BindingFlags AllMembers = Instance | Static | Public | NonPublic
Field Value
AllStaticMembers
Binding flags for all static members.
public const BindingFlags AllStaticMembers = Static | Public | NonPublic
Field Value
GetPrefix
Getters prefix.
public const string GetPrefix = "get_"
Field Value
IndexerName
The name for indexer members.
public const string IndexerName = "Item"
Field Value
Members
Attribute targets for fields and properties.
public const AttributeTargets Members = Property | Field
Field Value
RemovePrefix
Removers prefix.
public const string RemovePrefix = "remove_"
Field Value
SetPrefix
Setters prefix.
public const string SetPrefix = "set_"
Field Value
Types
Attribute targets for classes, structs, and interfaces.
public const AttributeTargets Types = Class | Struct | Interface
Field Value
Properties
CacheEnabled
Is cache enabled.
public static bool CacheEnabled { get; set; }
Property Value
ProxyTypes
Gets the proxy types.
public static IDictionary<Type, Type> ProxyTypes { get; }
Property Value
Methods
ClearCache()
Clear cache.
public static void ClearCache()
FilterMembers<T>(IEnumerable<T>, bool, bool?, params Type[])
Filters members based on inheritance, setter indication, and additional type parameters.
public static IEnumerable<T> FilterMembers<T>(this IEnumerable<T> members, bool useInheritance, bool? isSetter, params Type[] additionalTypes) where T : MemberInfo
Parameters
members
IEnumerable<T>The collection of members.
useInheritance
boolTrue to use inheritance comparison.
isSetter
bool?Indicates if filtering by setter should be applied.
additionalTypes
Type[]Additional types to match.
Returns
- IEnumerable<T>
An enumerable of filtered members.
Type Parameters
T
The member type.
FilterMembers<T>(IEnumerable<T>, bool?, params Type[])
Filters members based on setter indication and additional type parameters.
public static IEnumerable<T> FilterMembers<T>(this IEnumerable<T> members, bool? isSetter, params Type[] additionalTypes) where T : MemberInfo
Parameters
members
IEnumerable<T>The collection of members.
isSetter
bool?Indicates if filtering by setter should be applied.
additionalTypes
Type[]Additional types to match.
Returns
- IEnumerable<T>
An enumerable of filtered members.
Type Parameters
T
The member type.
FindImplementations<T>(Assembly, bool, bool, bool, Func<Type, bool>)
Find all T
implementation in the specified assembly.
public static IEnumerable<Type> FindImplementations<T>(this Assembly assembly, bool showObsolete = false, bool showNonPublic = false, bool showNonBrowsable = false, Func<Type, bool> extraFilter = null)
Parameters
assembly
AssemblyAssembly in where types scan required.
showObsolete
boolShow types marked as obsolete.
showNonPublic
boolShow non public types.
showNonBrowsable
boolShow types marked as non browsable.
extraFilter
Func<Type, bool>Extra filter.
Returns
- IEnumerable<Type>
Found types.
Type Parameters
T
Filter interface type.
GetAccessorOwner(MethodInfo)
Gets the owner of the accessor method.
public static MemberInfo GetAccessorOwner(this MethodInfo method)
Parameters
method
MethodInfo
Returns
GetArgTypes<TArg>(TArg)
Gets the argument types from the specified argument.
public static Type[] GetArgTypes<TArg>(TArg arg)
Parameters
arg
TArgThe argument.
Returns
- Type[]
An array of argument types.
Type Parameters
TArg
The type of the argument.
GetGenericType(Type, Type)
Gets the generic type from the target type based on the provided generic type definition.
public static Type GetGenericType(this Type targetType, Type genericType)
Parameters
Returns
- Type
The found generic type or null if not found.
GetGenericTypeArg(Type, Type, int)
Gets the specific generic argument type from the target type.
public static Type GetGenericTypeArg(this Type targetType, Type genericType, int index)
Parameters
targetType
TypeThe target type.
genericType
TypeThe generic type definition.
index
intThe index of the generic argument.
Returns
- Type
The generic argument type.
GetIndexer(Type, params Type[])
Gets the default indexer property of the specified type.
public static PropertyInfo GetIndexer(this Type type, params Type[] additionalTypes)
Parameters
Returns
- PropertyInfo
The PropertyInfo of the indexer.
GetIndexerTypes(PropertyInfo)
Gets the types of the parameters of the indexer property.
public static IEnumerable<Type> GetIndexerTypes(this PropertyInfo property)
Parameters
property
PropertyInfoThe indexer property.
Returns
- IEnumerable<Type>
An enumerable of parameter types.
GetIndexers(Type, params Type[])
Gets all indexer properties of the specified type.
public static PropertyInfo[] GetIndexers(this Type type, params Type[] additionalTypes)
Parameters
Returns
- PropertyInfo[]
An array with the indexer properties.
GetInvokeMethod(Type)
Gets the invoke method from the specified delegate type.
public static MethodInfo GetInvokeMethod(this Type delegType)
Parameters
delegType
TypeThe delegate type.
Returns
- MethodInfo
The MethodInfo representing the Invoke method.
GetItemType(Type)
Gets the item type for a collection type.
public static Type GetItemType(this Type collectionType)
Parameters
collectionType
TypeThe collection type.
Returns
- Type
The item type contained in the collection.
GetMemberType(MemberInfo)
Gets the type associated with the member.
public static Type GetMemberType(this MemberInfo member)
Parameters
member
MemberInfoThe member info.
Returns
- Type
The type of the member.
GetMember<T>(Type, string, BindingFlags, bool?, params Type[])
Gets a member info by name with specified binding flags.
public static T GetMember<T>(this Type type, string memberName, BindingFlags flags, bool? isSetter, params Type[] additionalTypes) where T : MemberInfo
Parameters
type
TypeThe target type.
memberName
stringThe member name.
flags
BindingFlagsBinding flags to use for lookup.
isSetter
bool?Indicates if it's a setter.
additionalTypes
Type[]Additional types to match.
Returns
- T
The member info.
Type Parameters
T
The member type.
GetMember<T>(Type, string, params Type[])
Gets a member info by name from the specified type.
public static T GetMember<T>(this Type type, string memberName, params Type[] additionalTypes) where T : MemberInfo
Parameters
type
TypeThe target type.
memberName
stringThe name of the member.
additionalTypes
Type[]Additional types to match.
Returns
- T
The member info.
Type Parameters
T
The member type.
GetMember<T>(Type, params Type[])
Gets a constructor member info from the specified type.
public static T GetMember<T>(this Type type, params Type[] additionalTypes) where T : ConstructorInfo
Parameters
Returns
- T
The member info.
Type Parameters
T
The member type.
GetMembers<T>(Type, BindingFlags, bool, string, bool?, params Type[])
Gets members by name with options for inheritance, setter indication, and additional types matching.
public static T[] GetMembers<T>(this Type type, BindingFlags flags, bool inheritance, string memberName, bool? isSetter, params Type[] additionalTypes) where T : MemberInfo
Parameters
type
TypeThe target type.
flags
BindingFlagsBinding flags for lookup.
inheritance
boolTrue to include inherited members.
memberName
stringThe member name.
isSetter
bool?Indicates if the member is a setter.
additionalTypes
Type[]Additional types to match.
Returns
- T[]
An array of matched members.
Type Parameters
T
The member type.
GetMembers<T>(Type, BindingFlags, bool, params Type[])
Gets members with an option for inheritance and additional types matching.
public static T[] GetMembers<T>(this Type type, BindingFlags flags, bool inheritance, params Type[] additionalTypes) where T : MemberInfo
Parameters
type
TypeThe target type.
flags
BindingFlagsBinding flags for lookup.
inheritance
boolTrue to include inherited members.
additionalTypes
Type[]Additional types to match.
Returns
- T[]
An array of matched members.
Type Parameters
T
The member type.
GetMembers<T>(Type, BindingFlags, params Type[])
Gets members with specified binding flags that match the additional types.
public static T[] GetMembers<T>(this Type type, BindingFlags flags, params Type[] additionalTypes) where T : MemberInfo
Parameters
type
TypeThe target type.
flags
BindingFlagsBinding flags for lookup.
additionalTypes
Type[]Additional types to match.
Returns
- T[]
An array of matched members.
Type Parameters
T
The member type.
GetMembers<T>(Type, params Type[])
Gets members from the type that match the additional types.
public static T[] GetMembers<T>(this Type type, params Type[] additionalTypes) where T : MemberInfo
Parameters
Returns
- T[]
An array of matched members.
Type Parameters
T
The member type.
GetParameterTypes(MethodBase)
Gets the parameter types for the specified method.
public static (ParameterInfo info, Type type)[] GetParameterTypes(this MethodBase method)
Parameters
method
MethodBaseThe method base.
Returns
- (ParameterInfo info, Type type)[]
An array of tuples with parameter info and its type.
GetParameterTypes(MethodBase, bool)
Gets the parameter types for the specified method.
public static (ParameterInfo info, Type type)[] GetParameterTypes(this MethodBase method, bool removeRef)
Parameters
method
MethodBaseThe method base.
removeRef
boolTrue to remove reference type wrappers.
Returns
- (ParameterInfo info, Type type)[]
An array of tuples with parameter info and its type.
IsAbstract(MemberInfo)
Determines whether the specified member is abstract.
public static bool IsAbstract(this MemberInfo member)
Parameters
member
MemberInfoThe member info.
Returns
- bool
True if the member is abstract; otherwise, false.
IsAssembly(string)
Determines whether the specified type is an assembly.
public static bool IsAssembly(this string dllName)
Parameters
dllName
string
Returns
IsCollection(Type)
Determines whether the specified type is a collection.
public static bool IsCollection(this Type type)
Parameters
type
TypeThe type to check.
Returns
- bool
True if the type is a collection; otherwise, false.
IsIndexer(MemberInfo)
Determines whether the specified member is an indexer.
public static bool IsIndexer(this MemberInfo member)
Parameters
member
MemberInfoThe member info.
Returns
- bool
True if the member is an indexer; otherwise, false.
IsIndexer(PropertyInfo)
Determines whether the specified property is an indexer.
public static bool IsIndexer(this PropertyInfo property)
Parameters
property
PropertyInfoThe property info.
Returns
- bool
True if the property is an indexer; otherwise, false.
IsMatch(FieldInfo, BindingFlags)
Determines the field is match.
public static bool IsMatch(this FieldInfo field, BindingFlags bindingFlags)
Parameters
field
FieldInfoThe field.
bindingFlags
BindingFlags
Returns
- bool
Operations result.
IsMatch(MethodBase, BindingFlags)
Determines the method is match.
public static bool IsMatch(this MethodBase methodInfo, BindingFlags bindingFlags)
Parameters
methodInfo
MethodBaseThe method info.
bindingFlags
BindingFlags
Returns
- bool
Operations result.
IsMatch(PropertyInfo, BindingFlags)
Determines the property is match.
public static bool IsMatch(this PropertyInfo propertyInfo, BindingFlags bindingFlags)
Parameters
propertyInfo
PropertyInfoThe property info.
bindingFlags
BindingFlags
Returns
- bool
Operations result.
IsModifiable(PropertyInfo)
Determines the property is modifiable.
public static bool IsModifiable(this PropertyInfo pi)
Parameters
pi
PropertyInfoThe property info.
Returns
- bool
Operations result.
IsOutput(ParameterInfo)
Determines whether the specified parameter is an output parameter.
public static bool IsOutput(this ParameterInfo param)
Parameters
param
ParameterInfoThe parameter info.
Returns
- bool
True if the parameter is output; otherwise, false.
IsOverloadable(MemberInfo)
Determines whether the specified member is overloadable.
public static bool IsOverloadable(this MemberInfo member)
Parameters
member
MemberInfoThe member info.
Returns
- bool
True if the member is overloadable; otherwise, false.
IsParams(ParameterInfo)
Determines whether the specified parameter is a params array.
public static bool IsParams(this ParameterInfo pi)
Parameters
pi
ParameterInfoThe parameter info.
Returns
- bool
True if the parameter is marked as params; otherwise, false.
IsRequiredType(Type, Type)
Is type compatible.
public static bool IsRequiredType(this Type type, Type required)
Parameters
Returns
- bool
Check result.
IsRequiredType<T>(Type)
Is type compatible.
public static bool IsRequiredType<T>(this Type type)
Parameters
type
TypeType.
Returns
- bool
Check result.
Type Parameters
T
Required type.
IsRuntimeType(Type)
Makes the generic method.
public static bool IsRuntimeType(this Type type)
Parameters
type
Type
Returns
IsStatic(MemberInfo)
Determines whether the specified member is static.
public static bool IsStatic(this MemberInfo member)
Parameters
member
MemberInfoThe member info.
Returns
- bool
True if the member is static; otherwise, false.
IsVirtual(MemberInfo)
Determines whether the specified member is virtual.
public static bool IsVirtual(this MemberInfo member)
Parameters
member
MemberInfoThe member info.
Returns
- bool
True if the member is virtual; otherwise, false.
Make(MethodInfo, params Type[])
Makes the generic method.
public static MethodInfo Make(this MethodInfo method, params Type[] types)
Parameters
method
MethodInfotypes
Type[]
Returns
MakePropertyName(string)
Makes the property name from the accessor name.
public static string MakePropertyName(this string accessorName)
Parameters
accessorName
stringThe accessor name.
Returns
- string
The property name.
MemberIs(MemberInfo, params MemberTypes[])
Determines whether the member has one of the specified member types.
public static bool MemberIs(this MemberInfo member, params MemberTypes[] types)
Parameters
member
MemberInfoThe member info.
types
MemberTypes[]The member types to check.
Returns
- bool
True if the member matches one of the types; otherwise, false.
OrderByDeclaration<TMember>(IEnumerable<TMember>)
Order the members by declaration.
public static IEnumerable<TMember> OrderByDeclaration<TMember>(this IEnumerable<TMember> members) where TMember : MemberInfo
Parameters
members
IEnumerable<TMember>Members.
Returns
- IEnumerable<TMember>
Ordered members.
Type Parameters
TMember
Member type.
TryFindType(IEnumerable<Type>, Func<Type, bool>, string)
Try find type.
public static Type TryFindType(this IEnumerable<Type> types, Func<Type, bool> isTypeCompatible, string typeName)
Parameters
types
IEnumerable<Type>Types.
isTypeCompatible
Func<Type, bool>Is type compatible.
typeName
stringThe type name.
Returns
- Type
The found type.
VerifyAssembly(string)
Verifies the assembly.
public static AssemblyName VerifyAssembly(this string dllName)
Parameters
dllName
string