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
membersIEnumerable<T>The collection of members.
useInheritanceboolTrue to use inheritance comparison.
isSetterbool?Indicates if filtering by setter should be applied.
additionalTypesType[]Additional types to match.
Returns
- IEnumerable<T>
An enumerable of filtered members.
Type Parameters
TThe 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
membersIEnumerable<T>The collection of members.
isSetterbool?Indicates if filtering by setter should be applied.
additionalTypesType[]Additional types to match.
Returns
- IEnumerable<T>
An enumerable of filtered members.
Type Parameters
TThe 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
assemblyAssemblyAssembly in where types scan required.
showObsoleteboolShow types marked as obsolete.
showNonPublicboolShow non public types.
showNonBrowsableboolShow types marked as non browsable.
extraFilterFunc<Type, bool>Extra filter.
Returns
- IEnumerable<Type>
Found types.
Type Parameters
TFilter interface type.
GetAccessorOwner(MethodInfo)
Gets the owner of the accessor method.
public static MemberInfo GetAccessorOwner(this MethodInfo method)
Parameters
methodMethodInfo
Returns
GetArgTypes<TArg>(TArg)
Gets the argument types from the specified argument.
public static Type[] GetArgTypes<TArg>(TArg arg)
Parameters
argTArgThe argument.
Returns
- Type[]
An array of argument types.
Type Parameters
TArgThe 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
targetTypeTypeThe target type.
genericTypeTypeThe generic type definition.
indexintThe 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
propertyPropertyInfoThe 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
delegTypeTypeThe 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
collectionTypeTypeThe 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
memberMemberInfoThe 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
typeTypeThe target type.
memberNamestringThe member name.
flagsBindingFlagsBinding flags to use for lookup.
isSetterbool?Indicates if it's a setter.
additionalTypesType[]Additional types to match.
Returns
- T
The member info.
Type Parameters
TThe 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
typeTypeThe target type.
memberNamestringThe name of the member.
additionalTypesType[]Additional types to match.
Returns
- T
The member info.
Type Parameters
TThe 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
TThe 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
typeTypeThe target type.
flagsBindingFlagsBinding flags for lookup.
inheritanceboolTrue to include inherited members.
memberNamestringThe member name.
isSetterbool?Indicates if the member is a setter.
additionalTypesType[]Additional types to match.
Returns
- T[]
An array of matched members.
Type Parameters
TThe 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
typeTypeThe target type.
flagsBindingFlagsBinding flags for lookup.
inheritanceboolTrue to include inherited members.
additionalTypesType[]Additional types to match.
Returns
- T[]
An array of matched members.
Type Parameters
TThe 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
typeTypeThe target type.
flagsBindingFlagsBinding flags for lookup.
additionalTypesType[]Additional types to match.
Returns
- T[]
An array of matched members.
Type Parameters
TThe 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
TThe member type.
GetParameterTypes(MethodBase)
Gets the parameter types for the specified method.
public static (ParameterInfo info, Type type)[] GetParameterTypes(this MethodBase method)
Parameters
methodMethodBaseThe 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
methodMethodBaseThe method base.
removeRefboolTrue 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
memberMemberInfoThe 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
dllNamestring
Returns
IsCollection(Type)
Determines whether the specified type is a collection.
public static bool IsCollection(this Type type)
Parameters
typeTypeThe 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
memberMemberInfoThe 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
propertyPropertyInfoThe 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
fieldFieldInfoThe field.
bindingFlagsBindingFlags
Returns
- bool
Operations result.
IsMatch(MethodBase, BindingFlags)
Determines the method is match.
public static bool IsMatch(this MethodBase methodInfo, BindingFlags bindingFlags)
Parameters
methodInfoMethodBaseThe method info.
bindingFlagsBindingFlags
Returns
- bool
Operations result.
IsMatch(PropertyInfo, BindingFlags)
Determines the property is match.
public static bool IsMatch(this PropertyInfo propertyInfo, BindingFlags bindingFlags)
Parameters
propertyInfoPropertyInfoThe property info.
bindingFlagsBindingFlags
Returns
- bool
Operations result.
IsModifiable(PropertyInfo)
Determines the property is modifiable.
public static bool IsModifiable(this PropertyInfo pi)
Parameters
piPropertyInfoThe 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
paramParameterInfoThe 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
memberMemberInfoThe 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
piParameterInfoThe 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
typeTypeType.
Returns
- bool
Check result.
Type Parameters
TRequired type.
IsRuntimeType(Type)
Makes the generic method.
public static bool IsRuntimeType(this Type type)
Parameters
typeType
Returns
IsStatic(MemberInfo)
Determines whether the specified member is static.
public static bool IsStatic(this MemberInfo member)
Parameters
memberMemberInfoThe 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
memberMemberInfoThe 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
methodMethodInfotypesType[]
Returns
MakePropertyName(string)
Makes the property name from the accessor name.
public static string MakePropertyName(this string accessorName)
Parameters
accessorNamestringThe 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
memberMemberInfoThe member info.
typesMemberTypes[]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
membersIEnumerable<TMember>Members.
Returns
- IEnumerable<TMember>
Ordered members.
Type Parameters
TMemberMember 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
typesIEnumerable<Type>Types.
isTypeCompatibleFunc<Type, bool>Is type compatible.
typeNamestringThe type name.
Returns
- Type
The found type.
VerifyAssembly(string)
Verifies the assembly.
public static AssemblyName VerifyAssembly(this string dllName)
Parameters
dllNamestring