Table of Contents

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

string

AllInstanceMembers

Binding flags for all instance members.

public const BindingFlags AllInstanceMembers = Instance | Public | NonPublic

Field Value

BindingFlags

AllMembers

Binding flags for all members.

public const BindingFlags AllMembers = Instance | Static | Public | NonPublic

Field Value

BindingFlags

AllStaticMembers

Binding flags for all static members.

public const BindingFlags AllStaticMembers = Static | Public | NonPublic

Field Value

BindingFlags

GetPrefix

Getters prefix.

public const string GetPrefix = "get_"

Field Value

string

IndexerName

The name for indexer members.

public const string IndexerName = "Item"

Field Value

string

Members

Attribute targets for fields and properties.

public const AttributeTargets Members = Property | Field

Field Value

AttributeTargets

RemovePrefix

Removers prefix.

public const string RemovePrefix = "remove_"

Field Value

string

SetPrefix

Setters prefix.

public const string SetPrefix = "set_"

Field Value

string

Types

Attribute targets for classes, structs, and interfaces.

public const AttributeTargets Types = Class | Struct | Interface

Field Value

AttributeTargets

Properties

CacheEnabled

Is cache enabled.

public static bool CacheEnabled { get; set; }

Property Value

bool

ProxyTypes

Gets the proxy types.

public static IDictionary<Type, Type> ProxyTypes { get; }

Property Value

IDictionary<Type, Type>

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 bool

True 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 Assembly

Assembly in where types scan required.

showObsolete bool

Show types marked as obsolete.

showNonPublic bool

Show non public types.

showNonBrowsable bool

Show 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

MemberInfo

GetArgTypes<TArg>(TArg)

Gets the argument types from the specified argument.

public static Type[] GetArgTypes<TArg>(TArg arg)

Parameters

arg TArg

The 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

targetType Type

The target type.

genericType Type

The generic type definition.

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 Type

The target type.

genericType Type

The generic type definition.

index int

The 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

type Type

The type to search.

additionalTypes Type[]

Additional types to match.

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 PropertyInfo

The 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

type Type

The type to search.

additionalTypes Type[]

Additional types to match.

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 Type

The 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 Type

The 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 MemberInfo

The 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 Type

The target type.

memberName string

The member name.

flags BindingFlags

Binding 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 Type

The target type.

memberName string

The 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

type Type

The target type.

additionalTypes Type[]

Additional types to match.

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 Type

The target type.

flags BindingFlags

Binding flags for lookup.

inheritance bool

True to include inherited members.

memberName string

The 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 Type

The target type.

flags BindingFlags

Binding flags for lookup.

inheritance bool

True 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 Type

The target type.

flags BindingFlags

Binding 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

type Type

The target type.

additionalTypes Type[]

Additional types to match.

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 MethodBase

The 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 MethodBase

The method base.

removeRef bool

True 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 MemberInfo

The 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

bool

IsCollection(Type)

Determines whether the specified type is a collection.

public static bool IsCollection(this Type type)

Parameters

type Type

The 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 MemberInfo

The 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 PropertyInfo

The 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 FieldInfo

The field.

bindingFlags 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 MethodBase

The method info.

bindingFlags 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 PropertyInfo

The property info.

bindingFlags BindingFlags

BindingFlags

Returns

bool

Operations result.

IsModifiable(PropertyInfo)

Determines the property is modifiable.

public static bool IsModifiable(this PropertyInfo pi)

Parameters

pi PropertyInfo

The 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 ParameterInfo

The 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 MemberInfo

The 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 ParameterInfo

The 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

type Type

Type.

required Type

Required type.

Returns

bool

Check result.

IsRequiredType<T>(Type)

Is type compatible.

public static bool IsRequiredType<T>(this Type type)

Parameters

type Type

Type.

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

bool

IsStatic(MemberInfo)

Determines whether the specified member is static.

public static bool IsStatic(this MemberInfo member)

Parameters

member MemberInfo

The 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 MemberInfo

The 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 MethodInfo
types Type[]

Returns

MethodInfo

MakePropertyName(string)

Makes the property name from the accessor name.

public static string MakePropertyName(this string accessorName)

Parameters

accessorName string

The 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 MemberInfo

The 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 string

The type name.

Returns

Type

The found type.

VerifyAssembly(string)

Verifies the assembly.

public static AssemblyName VerifyAssembly(this string dllName)

Parameters

dllName string

Returns

AssemblyName