Class ReflectionUtilities
Contains many utility functions for reflection
Namespace: WeaverCore.Utilities
Assembly: WeaverCore.dll
Syntax
public static class ReflectionUtilities
Methods
| Edit this page View SourceCreateFieldGetter<SourceType, FieldType>(FieldInfo)
Creates a function that retrives a field value
Declaration
public static Func<SourceType, FieldType> CreateFieldGetter<SourceType, FieldType>(FieldInfo field)
Parameters
Type | Name | Description |
---|---|---|
FieldInfo | field | The field to create a getter for |
Returns
Type | Description |
---|---|
Func<SourceType, FieldType> | Returns a function that when invoked, will retrieve the field value |
Type Parameters
Name | Description |
---|---|
SourceType | The type that contains the field |
FieldType | The type of the field |
CreateFieldGetter<SourceType, FieldType>(string, BindingFlags)
Creates a function that retrives a field value
Declaration
public static Func<SourceType, FieldType> CreateFieldGetter<SourceType, FieldType>(string fieldName, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
string | fieldName | The name of the field to create a getter for |
BindingFlags | flags | The binding flags used to find the field |
Returns
Type | Description |
---|---|
Func<SourceType, FieldType> | Returns a function that when invoked, will retrieve the field value |
Type Parameters
Name | Description |
---|---|
SourceType | The type that contains the field |
FieldType | The type of the field |
CreateFieldSetter<SourceType, FieldType>(FieldInfo)
Creates a funciton that sets a field value
Declaration
public static Action<SourceType, FieldType> CreateFieldSetter<SourceType, FieldType>(FieldInfo field)
Parameters
Type | Name | Description |
---|---|---|
FieldInfo | field | The field to create a setter for |
Returns
Type | Description |
---|---|
Action<SourceType, FieldType> | Returns a function that when invoked, will set the field value |
Type Parameters
Name | Description |
---|---|
SourceType | The type that contains the field |
FieldType | The type of the field |
CreateFieldSetter<SourceType, FieldType>(string, BindingFlags)
Creates a funciton that sets a field value
Declaration
public static Action<SourceType, FieldType> CreateFieldSetter<SourceType, FieldType>(string fieldName, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
string | fieldName | The name of the field to create a setter for |
BindingFlags | flags | The binding flags used to find the field |
Returns
Type | Description |
---|---|
Action<SourceType, FieldType> | Returns a function that when invoked, will set the field value |
Type Parameters
Name | Description |
---|---|
SourceType | The type that contains the field |
FieldType | The type of the field |
ExecuteMethodsWithAttribute<AttriType>(Func<MethodInfo, AttriType, bool>, BindingFlags, bool)
Finds all methods that have a certain attribute type applied to them, and executes them
Declaration
public static void ExecuteMethodsWithAttribute<AttriType>(Func<MethodInfo, AttriType, bool> ExecuteIf = null, BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, bool throwOnError = false) where AttriType : Attribute
Parameters
Type | Name | Description |
---|---|---|
Func<MethodInfo, AttriType, bool> | ExecuteIf | If a delegate is specified here, then only the methods that satify this delegate will be executed |
BindingFlags | flags | The binding flags to determine what kind of methods to find |
bool | throwOnError | Should an exception be thrown if a method fails? |
Type Parameters
Name | Description |
---|---|
AttriType | The type attribute to look for |
ExecuteMethodsWithAttribute<AttriType>(Assembly, Func<MethodInfo, AttriType, bool>, BindingFlags, bool)
Finds all methods in an assembly that have a certain attribute type applied to them, and executes them
Declaration
public static void ExecuteMethodsWithAttribute<AttriType>(Assembly assembly, Func<MethodInfo, AttriType, bool> ExecuteIf = null, BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, bool throwOnError = false) where AttriType : Attribute
Parameters
Type | Name | Description |
---|---|---|
Assembly | assembly | The assembly to look for the methods under |
Func<MethodInfo, AttriType, bool> | ExecuteIf | If a delegate is specified here, then only the methods that satify this delegate will be executed |
BindingFlags | flags | The binding flags to determine what kind of methods to find |
bool | throwOnError | Should an exception be thrown if a method fails? |
Type Parameters
Name | Description |
---|---|
AttriType | The type attribute to look for |
FindLoadedAssembly(string)
Finds a loaded assembly by its full name or simple name.
Declaration
public static Assembly FindLoadedAssembly(string assemblyName)
Parameters
Type | Name | Description |
---|---|---|
string | assemblyName | The full name or simple name of the assembly. |
Returns
Type | Description |
---|---|
Assembly | The loaded assembly or null if not found. |
GetChildrenOfType(Type, bool)
Retrives all types that inherit from parentType
Declaration
public static IEnumerable<Type> GetChildrenOfType(Type parentType, bool includeAbstract = false)
Parameters
Type | Name | Description |
---|---|---|
Type | parentType | The common base class to check for |
bool | includeAbstract | Should abstract classes also be included? |
Returns
Type | Description |
---|---|
IEnumerable<Type> | Returns a list of all types that inherit from |
GetChildrenOfType<T>(bool)
Retrives all types that inherit from type T
Declaration
public static IEnumerable<Type> GetChildrenOfType<T>(bool includeAbstract = false)
Parameters
Type | Name | Description |
---|---|---|
bool | includeAbstract | Should abstract classes also be included? |
Returns
Type | Description |
---|---|
IEnumerable<Type> | Returns a list of all types that inherit from type |
Type Parameters
Name | Description |
---|---|
T | The common base class to check for |
GetMethod<InstanceType>(string, BindingFlags)
Finds a method on a given type and returns a MethodInfo object for it
Declaration
public static MethodInfo GetMethod<InstanceType>(string methodName, BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
string | methodName | The name of the method to find |
BindingFlags | flags | The binding flags to find the method with |
Returns
Type | Description |
---|---|
MethodInfo | Returns a method info object for the fmound method |
Type Parameters
Name | Description |
---|---|
InstanceType | The instance type to find the method under |
GetMethodsWithAttribute<AttriType>(Assembly, BindingFlags)
Finds all methods in an assembly that have a certain attribute type applied to them
Declaration
public static IEnumerable<(MethodInfo method, AttriType attribute)> GetMethodsWithAttribute<AttriType>(Assembly assembly, BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) where AttriType : Attribute
Parameters
Type | Name | Description |
---|---|---|
Assembly | assembly | The assembly to look for the methods under |
BindingFlags | flags | The binding flags to determine what kind of methods to find |
Returns
Type | Description |
---|---|
IEnumerable<(MethodInfo method, AttriType attribute)> | Returns all methods in the assembly with the specified attribute type applied to them |
Type Parameters
Name | Description |
---|---|
AttriType | The type attribute to look for |
GetMethodsWithAttribute<AttriType>(Assembly, Type[], BindingFlags)
Finds all methods in an assembly that have a certain attribute type applied to them and accept certain parameters
Declaration
public static IEnumerable<(MethodInfo, AttriType)> GetMethodsWithAttribute<AttriType>(Assembly assembly, Type[] paramTypes, BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) where AttriType : Attribute
Parameters
Type | Name | Description |
---|---|---|
Assembly | assembly | The assembly to look for the methods under |
Type[] | paramTypes | The type of parameters that the methods must have |
BindingFlags | flags | The binding flags to determine what kind of methods to find |
Returns
Type | Description |
---|---|
IEnumerable<(MethodInfo method, AttriType attribute)> | Returns all methods with the specified attribute type applied to them |
Type Parameters
Name | Description |
---|---|
AttriType | The type attribute to look for |
GetMethodsWithAttribute<AttriType>(BindingFlags)
Finds all methods that have a certain attribute type applied to them
Declaration
public static IEnumerable<(MethodInfo method, AttriType attribute)> GetMethodsWithAttribute<AttriType>(BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) where AttriType : Attribute
Parameters
Type | Name | Description |
---|---|---|
BindingFlags | flags | The binding flags to determine what kind of methods to find |
Returns
Type | Description |
---|---|
IEnumerable<(MethodInfo method, AttriType attribute)> | Returns all methods with the specified attribute type applied to them |
Type Parameters
Name | Description |
---|---|
AttriType | The type attribute to look for |
GetMethodsWithAttribute<AttriType>(Type[], BindingFlags)
Finds all methods that have a certain attribute type applied to them and accept certain parameters
Declaration
public static IEnumerable<(MethodInfo method, AttriType attribute)> GetMethodsWithAttribute<AttriType>(Type[] paramTypes, BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) where AttriType : Attribute
Parameters
Type | Name | Description |
---|---|---|
Type[] | paramTypes | The type of parameters that the methods must have |
BindingFlags | flags | The binding flags to determine what kind of methods to find |
Returns
Type | Description |
---|---|
IEnumerable<(MethodInfo method, AttriType attribute)> | Returns all methods with the specified attribute type applied to them |
Type Parameters
Name | Description |
---|---|
AttriType | The type attribute to look for |
GetObjectsOfType<T>(bool)
Retrieves all types that inherit from type T
, and create instances of each of them
Declaration
public static IEnumerable<T> GetObjectsOfType<T>(bool throwOnFailure = false)
Parameters
Type | Name | Description |
---|---|---|
bool | throwOnFailure | If an object fails to instantiate, should an exception be thrown? If false, that object will be skipped over |
Returns
Type | Description |
---|---|
IEnumerable<T> | Returns a list of all objects that inherit from type |
Type Parameters
Name | Description |
---|---|
T | The common base class to check for |
MethodToDelegate<DelegateType>(MethodInfo)
Converts a MethodInfo object into a callable delegate
Declaration
public static DelegateType MethodToDelegate<DelegateType>(MethodInfo method)
Parameters
Type | Name | Description |
---|---|---|
MethodInfo | method | The method info |
Returns
Type | Description |
---|---|
DelegateType | The delegate that will call the method |
Type Parameters
Name | Description |
---|---|
DelegateType | The delegate type to convert type |
MethodToDelegate<DelegateType>(MethodInfo, object)
Converts a MethodInfo object into a callable delegate
Declaration
public static DelegateType MethodToDelegate<DelegateType>(MethodInfo method, object instance)
Parameters
Type | Name | Description |
---|---|---|
MethodInfo | method | The method info |
object | instance | The object instance that the method is bound to |
Returns
Type | Description |
---|---|
DelegateType | The delegate that will call the method |
Type Parameters
Name | Description |
---|---|
DelegateType | The delegate type to convert type |
MethodToDelegate<DelegateType, InstanceType>(string, BindingFlags)
Converts a MethodInfo object into a callable delegate
Declaration
public static DelegateType MethodToDelegate<DelegateType, InstanceType>(string methodName, BindingFlags Flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
string | methodName | The name of the method |
BindingFlags | Flags | The binding flags used to find the method on the object |
Returns
Type | Description |
---|---|
DelegateType | The delegate that will call the method |
Type Parameters
Name | Description |
---|---|
DelegateType | The delegate type to convert type |
InstanceType | The instance type the method is bound to |
MethodToDelegate<DelegateType, InstanceType>(string, InstanceType, BindingFlags)
Converts a MethodInfo object into a callable delegate
Declaration
public static DelegateType MethodToDelegate<DelegateType, InstanceType>(string methodName, InstanceType instance, BindingFlags Flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
string | methodName | The name of the method |
InstanceType | instance | The instance object the method is bound to |
BindingFlags | Flags | The binding flags used to find the method on the object |
Returns
Type | Description |
---|---|
DelegateType | The delegate that will call the method |
Type Parameters
Name | Description |
---|---|
DelegateType | The delegate type to convert type |
InstanceType | The instance type the method is bound to |
ReflectCallMethod(object, string, object[], BindingFlags)
Calls a method using reflection.
Declaration
public static object ReflectCallMethod(this object obj, string methodName, object[] parameters = null, BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object containing the method. |
string | methodName | The name of the method. |
object[] | parameters | An array of parameters to pass to the method. |
BindingFlags | flags | Binding flags for reflection (default is Static, NonPublic, Public, Instance). |
Returns
Type | Description |
---|---|
object | The result of the method invocation. |
ReflectGetField(object, string, BindingFlags)
Gets the value of a field using reflection.
Declaration
public static object ReflectGetField(this object obj, string fieldName, BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object containing the field. |
string | fieldName | The name of the field. |
BindingFlags | flags | Binding flags for reflection (default is Static, NonPublic, Public, Instance). |
Returns
Type | Description |
---|---|
object | The value of the specified field. |
ReflectGetMethod(object, string, BindingFlags)
Gets the MethodInfo object for a method using reflection.
Declaration
public static MethodInfo ReflectGetMethod(this object obj, string methodName, BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object containing the method. |
string | methodName | The name of the method. |
BindingFlags | flags | Binding flags for reflection (default is Static, NonPublic, Public, Instance). |
Returns
Type | Description |
---|---|
MethodInfo | The MethodInfo object for the specified method. |
ReflectGetProperty(object, string, BindingFlags)
Gets the value of a property using reflection.
Declaration
public static object ReflectGetProperty(this object obj, string propertyName, BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object containing the property. |
string | propertyName | The name of the property. |
BindingFlags | flags | Binding flags for reflection (default is Static, NonPublic, Public, Instance). |
Returns
Type | Description |
---|---|
object | The value of the specified property. |
ReflectSetField(object, string, object, BindingFlags)
Sets the value of a field using reflection.
Declaration
public static void ReflectSetField(this object obj, string fieldName, object value, BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object containing the field. |
string | fieldName | The name of the field. |
object | value | The value to set. |
BindingFlags | flags | Binding flags for reflection (default is Static, NonPublic, Public, Instance). |
ReflectSetProperty(object, string, object, BindingFlags)
Sets the value of a property using reflection.
Declaration
public static bool ReflectSetProperty(this object obj, string propertyName, object value, BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object containing the property. |
string | propertyName | The name of the property. |
object | value | The value to set. |
BindingFlags | flags | Binding flags for reflection (default is Static, NonPublic, Public, Instance). |
Returns
Type | Description |
---|---|
bool | True if the property was successfully set; otherwise, false. |