Class AccessTools
A helper class for reflection related functions
Inheritance
Namespace: HarmonyLib
Assembly: 0Harmony.dll
Syntax
public static class AccessTools
Fields
| Improve this Doc View Sourceall
Shortcut for System.Reflection.BindingFlags to simplify the use of reflections and make it work for any access level
Declaration
public static readonly BindingFlags all
Field Value
Type | Description |
---|---|
System.Reflection.BindingFlags |
allDeclared
Shortcut for System.Reflection.BindingFlags to simplify the use of reflections and make it work for any access level but only within the current type
Declaration
public static readonly BindingFlags allDeclared
Field Value
Type | Description |
---|---|
System.Reflection.BindingFlags |
Properties
| Improve this Doc View SourceIsMonoRuntime
True if the current runtime is based on Mono, false otherwise (.NET)
Declaration
public static bool IsMonoRuntime { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsNetCoreRuntime
True if the current runtime is .NET Core, false otherwise (Mono or .NET Framework)
Declaration
public static bool IsNetCoreRuntime { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsNetFrameworkRuntime
True if the current runtime is .NET Framework, false otherwise (.NET Core or Mono, although latter isn't guaranteed)
Declaration
public static bool IsNetFrameworkRuntime { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Methods
| Improve this Doc View SourceActualParameters(MethodBase, Object[])
Creates an array of input parameters for a given method and a given set of potential inputs
Declaration
public static object[] ActualParameters(MethodBase method, object[] inputs)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodBase | method | The method/constructor you are planing to call |
System.Object[] | inputs | The possible input parameters in any order |
Returns
Type | Description |
---|---|
System.Object[] | An object array matching the method signature |
AllAssemblies()
Enumerates all assemblies in the current app domain, excluding visual studio assemblies
Declaration
public static IEnumerable<Assembly> AllAssemblies()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Reflection.Assembly> | An enumeration of System.Reflection.Assembly |
AllTypes()
Enumerates all successfully loaded types in the current app domain, excluding visual studio assemblies
Declaration
public static IEnumerable<Type> AllTypes()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Type> | An enumeration of all System.Type in all assemblies, excluding visual studio assemblies |
CombinedHashCode(IEnumerable<Object>)
Calculates a combined hash code for an enumeration of objects
Declaration
public static int CombinedHashCode(IEnumerable<object> objects)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.Object> | objects | The objects |
Returns
Type | Description |
---|---|
System.Int32 | The hash code |
Constructor(Type, Type[], Boolean)
Gets the reflection information for a constructor by searching the type and all its super types
Declaration
public static ConstructorInfo Constructor(Type type, Type[] parameters = null, bool searchForStatic = false)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the constructor is declared |
System.Type[] | parameters | Optional parameters to target a specific overload of the method |
System.Boolean | searchForStatic | Optional parameters to only consider static constructors |
Returns
Type | Description |
---|---|
System.Reflection.ConstructorInfo | A constructor info or null when type is null or when the method cannot be found |
CreateInstance(Type)
Creates an (possibly uninitialized) instance of a given type
Declaration
public static object CreateInstance(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type |
Returns
Type | Description |
---|---|
System.Object | The new instance |
CreateInstance<T>()
Creates an (possibly uninitialized) instance of a given type
Declaration
public static T CreateInstance<T>()
Returns
Type | Description |
---|---|
T | The new instance |
Type Parameters
Name | Description |
---|---|
T | The class/type |
DeclaredConstructor(Type, Type[], Boolean)
Gets the reflection information for a directly declared constructor
Declaration
public static ConstructorInfo DeclaredConstructor(Type type, Type[] parameters = null, bool searchForStatic = false)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the constructor is declared |
System.Type[] | parameters | Optional parameters to target a specific overload of the constructor |
System.Boolean | searchForStatic | Optional parameters to only consider static constructors |
Returns
Type | Description |
---|---|
System.Reflection.ConstructorInfo | A constructor info or null when type is null or when the constructor cannot be found |
DeclaredField(String)
Gets the reflection information for a directly declared field
Declaration
public static FieldInfo DeclaredField(string typeColonName)
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The member in the form |
Returns
Type | Description |
---|---|
System.Reflection.FieldInfo | A field or null when the field cannot be found |
DeclaredField(Type, Int32)
Gets the reflection information for a field
Declaration
public static FieldInfo DeclaredField(Type type, int idx)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the field is declared |
System.Int32 | idx | The zero-based index of the field inside the class definition |
Returns
Type | Description |
---|---|
System.Reflection.FieldInfo | A field or null when type is null or when the field cannot be found |
DeclaredField(Type, String)
Gets the reflection information for a directly declared field
Declaration
public static FieldInfo DeclaredField(Type type, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the field is defined |
System.String | name | The name of the field |
Returns
Type | Description |
---|---|
System.Reflection.FieldInfo | A field or null when type/name is null or when the field cannot be found |
DeclaredIndexer(Type, Type[])
Gets the reflection information for a directly declared indexer property
Declaration
public static PropertyInfo DeclaredIndexer(Type type, Type[] parameters = null)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the indexer property is declared |
System.Type[] | parameters | Optional parameters to target a specific overload of multiple indexers |
Returns
Type | Description |
---|---|
System.Reflection.PropertyInfo | An indexer property or null when type is null or when it cannot be found |
DeclaredIndexerGetter(Type, Type[])
Gets the reflection information for the getter method of a directly declared indexer property
Declaration
public static MethodInfo DeclaredIndexerGetter(Type type, Type[] parameters = null)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the indexer property is declared |
System.Type[] | parameters | Optional parameters to target a specific overload of multiple indexers |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when type is null or when indexer property cannot be found |
DeclaredIndexerSetter(Type, Type[])
Gets the reflection information for the setter method of a directly declared indexer property
Declaration
public static MethodInfo DeclaredIndexerSetter(Type type, Type[] parameters)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the indexer property is declared |
System.Type[] | parameters | Optional parameters to target a specific overload of multiple indexers |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when type is null or when indexer property cannot be found |
DeclaredMethod(String, Type[], Type[])
Gets the reflection information for a directly declared method
Declaration
public static MethodInfo DeclaredMethod(string typeColonName, Type[] parameters = null, Type[] generics = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The member in the form |
System.Type[] | parameters | Optional parameters to target a specific overload of the method |
System.Type[] | generics | Optional list of types that define the generic version of the method |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when the method cannot be found |
DeclaredMethod(Type, String, Type[], Type[])
Gets the reflection information for a directly declared method
Declaration
public static MethodInfo DeclaredMethod(Type type, string name, Type[] parameters = null, Type[] generics = null)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the method is declared |
System.String | name | The name of the method (case sensitive) |
System.Type[] | parameters | Optional parameters to target a specific overload of the method |
System.Type[] | generics | Optional list of types that define the generic version of the method |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when type/name is null or when the method cannot be found |
DeclaredProperty(String)
Gets the reflection information for a directly declared property
Declaration
public static PropertyInfo DeclaredProperty(string typeColonName)
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The member in the form |
Returns
Type | Description |
---|---|
System.Reflection.PropertyInfo | A property or null when the property cannot be found |
DeclaredProperty(Type, String)
Gets the reflection information for a directly declared property
Declaration
public static PropertyInfo DeclaredProperty(Type type, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the property is declared |
System.String | name | The name of the property (case sensitive) |
Returns
Type | Description |
---|---|
System.Reflection.PropertyInfo | A property or null when type/name is null or when the property cannot be found |
DeclaredPropertyGetter(String)
Gets the reflection information for the getter method of a directly declared property
Declaration
public static MethodInfo DeclaredPropertyGetter(string typeColonName)
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The member in the form |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when the property cannot be found |
DeclaredPropertyGetter(Type, String)
Gets the reflection information for the getter method of a directly declared property
Declaration
public static MethodInfo DeclaredPropertyGetter(Type type, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the property is declared |
System.String | name | The name of the property (case sensitive) |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when type/name is null or when the property cannot be found |
DeclaredPropertySetter(String)
Gets the reflection information for the Setter method of a directly declared property
Declaration
public static MethodInfo DeclaredPropertySetter(string typeColonName)
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The member in the form |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when the property cannot be found |
DeclaredPropertySetter(Type, String)
Gets the reflection information for the setter method of a directly declared property
Declaration
public static MethodInfo DeclaredPropertySetter(Type type, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the property is declared |
System.String | name | The name of the property (case sensitive) |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when type/name is null or when the property cannot be found |
EnumeratorMoveNext(MethodBase)
Gets the System.Collections.IEnumerator.MoveNext method of an enumerator method
Declaration
public static MethodInfo EnumeratorMoveNext(MethodBase method)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodBase | method | Enumerator method that creates the enumerator System.Collections.IEnumerator |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | The internal System.Collections.IEnumerator.MoveNext method of the enumerator or null if no valid enumerator is detected |
Field(String)
Gets the reflection information for a field by searching the type and all its super types
Declaration
public static FieldInfo Field(string typeColonName)
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The member in the form |
Returns
Type | Description |
---|---|
System.Reflection.FieldInfo | A field or null when the field cannot be found |
Field(Type, String)
Gets the reflection information for a field by searching the type and all its super types
Declaration
public static FieldInfo Field(Type type, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the field is defined |
System.String | name | The name of the field (case sensitive) |
Returns
Type | Description |
---|---|
System.Reflection.FieldInfo | A field or null when type/name is null or when the field cannot be found |
FieldRefAccess<F>(String)
Creates a field reference delegate for an instance field of a class or static field (NOT an instance field of a struct)
Declaration
public static AccessTools.FieldRef<object, F> FieldRefAccess<F>(string typeColonName)
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The member in the form |
Returns
Type | Description |
---|---|
AccessTools.FieldRef<System.Object, F> | A readable/assignable AccessTools.FieldRef<T, F> delegate with |
Type Parameters
Name | Description |
---|---|
F | type of the field |
FieldRefAccess<F>(Type, String)
Creates a field reference delegate for an instance field of a class or static field (NOT an instance field of a struct)
Declaration
public static AccessTools.FieldRef<object, F> FieldRefAccess<F>(Type type, string fieldName)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type that defines the field, or derived class of this type; must not be a struct type unless the field is static |
System.String | fieldName | The name of the field |
Returns
Type | Description |
---|---|
AccessTools.FieldRef<System.Object, F> | A readable/assignable AccessTools.FieldRef<T, F> delegate with |
Type Parameters
Name | Description |
---|---|
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
Remarks
This method is meant for cases where the given type is only known at runtime and thus can't be used as a type parameter T
in e.g. FieldRefAccess<T, F>(String).
This method supports static fields, even those defined in structs, for legacy reasons. Consider using StaticFieldRefAccess<F>(Type, String) (and other overloads) instead for static fields.
FieldRefAccess<T, F>(T, FieldInfo)
Creates a field reference for an instance field of a class
Declaration
public static ref F FieldRefAccess<T, F>(T instance, FieldInfo fieldInfo)
Parameters
Type | Name | Description |
---|---|---|
T | instance | The instance |
System.Reflection.FieldInfo | fieldInfo | The field |
Returns
Type | Description |
---|---|
F | A readable/assignable reference to the field |
Type Parameters
Name | Description |
---|---|
T | The type that defines the field; or a parent class (including System.Object), implemented interface, or derived class of this type
(" |
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
Remarks
This method is meant for one-off access to a field's value for a single instance and where the field has already been obtained.
If you need to access a field's value for potentially multiple instances, use FieldRefAccess<T, F>(FieldInfo) instead.
FieldRefAccess<T, F>(instance, fieldInfo)
is functionally equivalent to FieldRefAccess<T, F>(fieldInfo)(instance)
.
For backwards compatibility, there is no class constraint on T
.
Instead, the non-value-type check is done at runtime within the method.
FieldRefAccess<T, F>(T, String)
Creates an instance field reference for a specific instance of a class
Declaration
public static ref F FieldRefAccess<T, F>(T instance, string fieldName)
Parameters
Type | Name | Description |
---|---|---|
T | instance | The instance |
System.String | fieldName | The name of the field |
Returns
Type | Description |
---|---|
F | A readable/assignable reference to the field |
Type Parameters
Name | Description |
---|---|
T | The class that defines the instance field, or derived class of this type |
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
Remarks
This method is meant for one-off access to a field's value for a single instance.
If you need to access a field's value for potentially multiple instances, use FieldRefAccess<T, F>(String) instead.
FieldRefAccess<T, F>(instance, fieldName)
is functionally equivalent to FieldRefAccess<T, F>(fieldName)(instance)
.
For backwards compatibility, there is no class constraint on T
.
Instead, the non-value-type check is done at runtime within the method.
FieldRefAccess<T, F>(FieldInfo)
Creates a field reference delegate for an instance field of a class or static field (NOT an instance field of a struct)
Declaration
public static AccessTools.FieldRef<T, F> FieldRefAccess<T, F>(FieldInfo fieldInfo)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.FieldInfo | fieldInfo | The field |
Returns
Type | Description |
---|---|
AccessTools.FieldRef<T, F> | A readable/assignable AccessTools.FieldRef<T, F> delegate |
Type Parameters
Name | Description |
---|---|
T | An arbitrary type if the field is static; otherwise the class that defines the field, or a parent class (including System.Object),
implemented interface, or derived class of this type (" |
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
Remarks
This method is meant for cases where the field has already been obtained, avoiding the field searching cost in e.g. FieldRefAccess<T, F>(String).
This method supports static fields, even those defined in structs, for legacy reasons.
For such static fields, T
is effectively ignored.
Consider using StaticFieldRefAccess<T, F>(FieldInfo) (and other overloads) instead for static fields.
For backwards compatibility, there is no class constraint on T
.
Instead, the non-value-type check is done at runtime within the method.
FieldRefAccess<T, F>(String)
Creates a field reference delegate for an instance field of a class
Declaration
public static AccessTools.FieldRef<T, F> FieldRefAccess<T, F>(string fieldName)
Parameters
Type | Name | Description |
---|---|---|
System.String | fieldName | The name of the field |
Returns
Type | Description |
---|---|
AccessTools.FieldRef<T, F> | A readable/assignable AccessTools.FieldRef<T, F> delegate |
Type Parameters
Name | Description |
---|---|
T | The class that defines the instance field, or derived class of this type |
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
Remarks
For backwards compatibility, there is no class constraint on T
.
Instead, the non-value-type check is done at runtime within the method.
FindIncludingBaseTypes<T>(Type, Func<Type, T>)
Applies a function going up the type hierarchy and stops at the first non-null
result
Declaration
public static T FindIncludingBaseTypes<T>(Type type, Func<Type, T> func)
where T : class
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type to start with |
System.Func<System.Type, T> | func | The evaluation function returning T |
Returns
Type | Description |
---|---|
T | The first non- |
Type Parameters
Name | Description |
---|---|
T | Result type of func() |
Remarks
The type hierarchy of a class or value type (including struct) does NOT include implemented interfaces, and the type hierarchy of an interface is only itself (regardless of whether that interface implements other interfaces). The top-most type in the type hierarchy of all non-interface types (including value types) is System.Object.
FindIncludingInnerTypes<T>(Type, Func<Type, T>)
Applies a function going into inner types and stops at the first non-null
result
Declaration
public static T FindIncludingInnerTypes<T>(Type type, Func<Type, T> func)
where T : class
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type to start with |
System.Func<System.Type, T> | func | The evaluation function returning T |
Returns
Type | Description |
---|---|
T | The first non- |
Type Parameters
Name | Description |
---|---|
T | Generic type parameter |
FirstConstructor(Type, Func<ConstructorInfo, Boolean>)
Given a type, returns the first constructor matching a predicate
Declaration
public static ConstructorInfo FirstConstructor(Type type, Func<ConstructorInfo, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type to start searching at |
System.Func<System.Reflection.ConstructorInfo, System.Boolean> | predicate | The predicate to search with |
Returns
Type | Description |
---|---|
System.Reflection.ConstructorInfo | The constructor info or null if type/predicate is null or if a type with that name cannot be found |
FirstInner(Type, Func<Type, Boolean>)
Given a type, returns the first inner type matching a recursive search with a predicate
Declaration
public static Type FirstInner(Type type, Func<Type, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type to start searching at |
System.Func<System.Type, System.Boolean> | predicate | The predicate to search with |
Returns
Type | Description |
---|---|
System.Type | The inner type or null if type/predicate is null or if a type with that name cannot be found |
FirstMethod(Type, Func<MethodInfo, Boolean>)
Given a type, returns the first method matching a predicate
Declaration
public static MethodInfo FirstMethod(Type type, Func<MethodInfo, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type to start searching at |
System.Func<System.Reflection.MethodInfo, System.Boolean> | predicate | The predicate to search with |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | The method or null if type/predicate is null or if a type with that name cannot be found |
FirstProperty(Type, Func<PropertyInfo, Boolean>)
Given a type, returns the first property matching a predicate
Declaration
public static PropertyInfo FirstProperty(Type type, Func<PropertyInfo, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type to start searching at |
System.Func<System.Reflection.PropertyInfo, System.Boolean> | predicate | The predicate to search with |
Returns
Type | Description |
---|---|
System.Reflection.PropertyInfo | The property or null if type/predicate is null or if a type with that name cannot be found |
GetDeclaredConstructors(Type, Nullable<Boolean>)
Gets reflection information for all declared constructors
Declaration
public static List<ConstructorInfo> GetDeclaredConstructors(Type type, bool? searchForStatic = null)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the constructors are declared |
System.Nullable<System.Boolean> | searchForStatic | Optional parameters to only consider static constructors |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.Reflection.ConstructorInfo> | A list of constructor infos |
GetDeclaredFields(Type)
Gets reflection information for all declared fields
Declaration
public static List<FieldInfo> GetDeclaredFields(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the fields are declared |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.Reflection.FieldInfo> | A list of fields |
GetDeclaredMember<T>(T)
Gets the real implementation of a class member
Declaration
public static T GetDeclaredMember<T>(this T member)
where T : MemberInfo
Parameters
Type | Name | Description |
---|---|---|
T | member | A member |
Returns
Type | Description |
---|---|
T | The member itself if its declared. Otherwise the member that is actually implemented in some base type |
Type Parameters
Name | Description |
---|---|
T |
GetDeclaredMethods(Type)
Gets reflection information for all declared methods
Declaration
public static List<MethodInfo> GetDeclaredMethods(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the methods are declared |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.Reflection.MethodInfo> | A list of methods |
GetDeclaredProperties(Type)
Gets reflection information for all declared properties
Declaration
public static List<PropertyInfo> GetDeclaredProperties(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the properties are declared |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.Reflection.PropertyInfo> | A list of properties |
GetDefaultValue(Type)
Gets default value for a specific type
Declaration
public static object GetDefaultValue(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type |
Returns
Type | Description |
---|---|
System.Object | The default value |
GetFieldNames(Object)
Gets the names of all fields that are declared in the type of the instance
Declaration
public static List<string> GetFieldNames(object instance)
Parameters
Type | Name | Description |
---|---|---|
System.Object | instance | An instance of the type to search in |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.String> | A list of field names |
GetFieldNames(Type)
Gets the names of all fields that are declared in a type
Declaration
public static List<string> GetFieldNames(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The declaring class/type |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.String> | A list of field names |
GetMethodNames(Object)
Gets the names of all method that are declared in the type of the instance
Declaration
public static List<string> GetMethodNames(object instance)
Parameters
Type | Name | Description |
---|---|---|
System.Object | instance | An instance of the type to search in |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.String> | A list of method names |
GetMethodNames(Type)
Gets the names of all method that are declared in a type
Declaration
public static List<string> GetMethodNames(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The declaring class/type |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.String> | A list of method names |
GetOutsideCaller()
Returns who called the current method
Declaration
public static MethodBase GetOutsideCaller()
Returns
Type | Description |
---|---|
System.Reflection.MethodBase | The calling method/constructor (excluding the caller) |
GetPropertyNames(Object)
Gets the names of all properties that are declared in the type of the instance
Declaration
public static List<string> GetPropertyNames(object instance)
Parameters
Type | Name | Description |
---|---|---|
System.Object | instance | An instance of the type to search in |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.String> | A list of property names |
GetPropertyNames(Type)
Gets the names of all properties that are declared in a type
Declaration
public static List<string> GetPropertyNames(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The declaring class/type |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.String> | A list of property names |
GetReturnedType(MethodBase)
Gets the return type of a method or constructor
Declaration
public static Type GetReturnedType(MethodBase methodOrConstructor)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodBase | methodOrConstructor | The method/constructor |
Returns
Type | Description |
---|---|
System.Type | The return type |
GetTypes(Object[])
Returns an array containing the type of each object in the given array
Declaration
public static Type[] GetTypes(object[] parameters)
Parameters
Type | Name | Description |
---|---|---|
System.Object[] | parameters | An array of objects |
Returns
Type | Description |
---|---|
System.Type[] | An array of types or an empty array if parameters is null (if an object is null, the type for it will be object) |
GetTypesFromAssembly(Assembly)
Gets all successfully loaded types from a given assembly
Declaration
public static Type[] GetTypesFromAssembly(Assembly assembly)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.Assembly | assembly | The assembly |
Returns
Type | Description |
---|---|
System.Type[] | An array of types |
Remarks
This calls and returns System.Reflection.Assembly.GetTypes, while catching any thrown System.Reflection.ReflectionTypeLoadException. If such an exception is thrown, returns the successfully loaded types (System.Reflection.ReflectionTypeLoadException.Types, filtered for non-null values).
GetUnderlyingType(MemberInfo)
Gets the type of any class member of
Declaration
public static Type GetUnderlyingType(this MemberInfo member)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MemberInfo | member | A member |
Returns
Type | Description |
---|---|
System.Type | The class/type of this member |
HarmonyDelegate<DelegateType>(Object)
Creates a delegate for a given delegate definition, attributed with [HarmonyDelegate]
Declaration
public static DelegateType HarmonyDelegate<DelegateType>(object instance = null)
where DelegateType : Delegate
Parameters
Type | Name | Description |
---|---|---|
System.Object | instance | Only applies for instance methods. If |
Returns
Type | Description |
---|---|
DelegateType | A delegate of given |
Type Parameters
Name | Description |
---|---|
DelegateType | The delegate Type, attributed with [HarmonyDelegate] |
Remarks
This calls MethodDelegate<DelegateType>(MethodInfo, Object, Boolean) with the method
and virtualCall
arguments
determined from the [HarmonyDelegate] attributes on DelegateType
,
and the given instance
(for closed instance delegates).
Identifiable(MethodInfo)
Creates an identifiable version of a method
Declaration
public static MethodInfo Identifiable(this MethodInfo method)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodInfo | method | The method |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo |
Indexer(Type, Type[])
Gets the reflection information for an indexer property by searching the type and all its super types
Declaration
public static PropertyInfo Indexer(Type type, Type[] parameters = null)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type |
System.Type[] | parameters | Optional parameters to target a specific overload of multiple indexers |
Returns
Type | Description |
---|---|
System.Reflection.PropertyInfo | An indexer property or null when type is null or when it cannot be found |
IndexerGetter(Type, Type[])
Gets the reflection information for the getter method of an indexer property by searching the type and all its super types
Declaration
public static MethodInfo IndexerGetter(Type type, Type[] parameters = null)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type |
System.Type[] | parameters | Optional parameters to target a specific overload of multiple indexers |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when type is null or when the indexer property cannot be found |
IndexerSetter(Type, Type[])
Gets the reflection information for the setter method of an indexer property by searching the type and all its super types
Declaration
public static MethodInfo IndexerSetter(Type type, Type[] parameters = null)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type |
System.Type[] | parameters | Optional parameters to target a specific overload of multiple indexers |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when type is null or when the indexer property cannot be found |
Inner(Type, String)
Given a type, returns the first inner type matching a recursive search by name
Declaration
public static Type Inner(Type type, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type to start searching at |
System.String | name | The name of the inner type (case sensitive) |
Returns
Type | Description |
---|---|
System.Type | The inner type or null if type/name is null or if a type with that name cannot be found |
InnerTypes(Type)
Enumerates all inner types (non-recursive) of a given type
Declaration
public static IEnumerable<Type> InnerTypes(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type to start with |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Type> | An enumeration of all inner System.Type |
IsClass(Type)
Tests if a type is a class
Declaration
public static bool IsClass(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type |
Returns
Type | Description |
---|---|
System.Boolean | True if the type is a class |
IsDeclaredMember<T>(T)
Test if a class member is actually an concrete implementation
Declaration
public static bool IsDeclaredMember<T>(this T member)
where T : MemberInfo
Parameters
Type | Name | Description |
---|---|---|
T | member | A member |
Returns
Type | Description |
---|---|
System.Boolean | True if the member is a declared |
Type Parameters
Name | Description |
---|---|
T |
IsFloatingPoint(Type)
Tests if a type is a floating point type
Declaration
public static bool IsFloatingPoint(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type |
Returns
Type | Description |
---|---|
System.Boolean | True if the type represents some floating point |
IsInteger(Type)
Tests if a type is an integer type
Declaration
public static bool IsInteger(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type |
Returns
Type | Description |
---|---|
System.Boolean | True if the type represents some integer |
IsNumber(Type)
Tests if a type is a numerical type
Declaration
public static bool IsNumber(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type |
Returns
Type | Description |
---|---|
System.Boolean | True if the type represents some number |
IsOfNullableType<T>(T)
Test whether an instance is of a nullable type
Declaration
public static bool IsOfNullableType<T>(T instance)
Parameters
Type | Name | Description |
---|---|---|
T | instance | An instance to test |
Returns
Type | Description |
---|---|
System.Boolean | True if instance is of nullable type, false if not |
Type Parameters
Name | Description |
---|---|
T | Type of instance |
IsStatic(MemberInfo)
Tests whether a type or member is static, as defined in C#
Declaration
public static bool IsStatic(MemberInfo member)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MemberInfo | member | The type or member |
Returns
Type | Description |
---|---|
System.Boolean | True if the type or member is static |
IsStruct(Type)
Tests if a type is a struct
Declaration
public static bool IsStruct(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type |
Returns
Type | Description |
---|---|
System.Boolean | True if the type is a struct |
IsValue(Type)
Tests if a type is a value type
Declaration
public static bool IsValue(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type |
Returns
Type | Description |
---|---|
System.Boolean | True if the type is a value type |
IsVoid(Type)
Tests if a type is void
Declaration
public static bool IsVoid(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type |
Returns
Type | Description |
---|---|
System.Boolean | True if the type is void |
MakeDeepCopy(Object, Type, Func<String, Traverse, Traverse, Object>, String)
Makes a deep copy of any object
Declaration
public static object MakeDeepCopy(object source, Type resultType, Func<string, Traverse, Traverse, object> processor = null, string pathRoot = "")
Parameters
Type | Name | Description |
---|---|---|
System.Object | source | The original object |
System.Type | resultType | The type of the instance that should be created |
System.Func<System.String, Traverse, Traverse, System.Object> | processor | Optional value transformation function (taking a field name and src/dst Traverse instances) |
System.String | pathRoot | The optional path root to start with |
Returns
Type | Description |
---|---|
System.Object | The copy of the original object |
MakeDeepCopy<T>(Object)
Makes a deep copy of any object
Declaration
public static T MakeDeepCopy<T>(object source)
where T : class
Parameters
Type | Name | Description |
---|---|---|
System.Object | source | The original object |
Returns
Type | Description |
---|---|
T | A copy of the original object but of type T |
Type Parameters
Name | Description |
---|---|
T | The type of the instance that should be created; for legacy reasons, this must be a class or interface |
MakeDeepCopy<T>(Object, out T, Func<String, Traverse, Traverse, Object>, String)
Makes a deep copy of any object
Declaration
public static void MakeDeepCopy<T>(object source, out T result, Func<string, Traverse, Traverse, object> processor = null, string pathRoot = "")
Parameters
Type | Name | Description |
---|---|---|
System.Object | source | The original object |
T | result | [out] The copy of the original object |
System.Func<System.String, Traverse, Traverse, System.Object> | processor | Optional value transformation function (taking a field name and src/dst Traverse instances) |
System.String | pathRoot | The optional path root to start with |
Type Parameters
Name | Description |
---|---|
T | The type of the instance that should be created |
Method(String, Type[], Type[])
Gets the reflection information for a method by searching the type and all its super types
Declaration
public static MethodInfo Method(string typeColonName, Type[] parameters = null, Type[] generics = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The member in the form |
System.Type[] | parameters | Optional parameters to target a specific overload of the method |
System.Type[] | generics | Optional list of types that define the generic version of the method |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when the method cannot be found |
Method(Type, String, Type[], Type[])
Gets the reflection information for a method by searching the type and all its super types
Declaration
public static MethodInfo Method(Type type, string name, Type[] parameters = null, Type[] generics = null)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type where the method is declared |
System.String | name | The name of the method (case sensitive) |
System.Type[] | parameters | Optional parameters to target a specific overload of the method |
System.Type[] | generics | Optional list of types that define the generic version of the method |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when type/name is null or when the method cannot be found |
MethodDelegate<DelegateType>(MethodInfo, Object, Boolean)
Creates a delegate to a given method
Declaration
public static DelegateType MethodDelegate<DelegateType>(MethodInfo method, object instance = null, bool virtualCall = true)
where DelegateType : Delegate
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodInfo | method | The method to create a delegate from. |
System.Object | instance | Only applies for instance methods. If |
System.Boolean | virtualCall | Only applies for instance methods. If |
Returns
Type | Description |
---|---|
DelegateType | A delegate of given |
Type Parameters
Name | Description |
---|---|
DelegateType | The delegate Type |
Remarks
Delegate invocation is more performant and more convenient to use than System.Reflection.MethodBase.Invoke(System.Object,System.Object[]) at a one-time setup cost.
Works for both type of static and instance methods, both open and closed (a.k.a. unbound and bound) instance methods, and both class and struct methods.
MethodDelegate<DelegateType>(String, Object, Boolean)
Creates a delegate to a given method
Declaration
public static DelegateType MethodDelegate<DelegateType>(string typeColonName, object instance = null, bool virtualCall = true)
where DelegateType : Delegate
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The method in the form |
System.Object | instance | Only applies for instance methods. If |
System.Boolean | virtualCall | Only applies for instance methods. If |
Returns
Type | Description |
---|---|
DelegateType | A delegate of given |
Type Parameters
Name | Description |
---|---|
DelegateType | The delegate Type |
Remarks
Delegate invocation is more performant and more convenient to use than System.Reflection.MethodBase.Invoke(System.Object,System.Object[]) at a one-time setup cost.
Works for both type of static and instance methods, both open and closed (a.k.a. unbound and bound) instance methods, and both class and struct methods.
Property(String)
Gets the reflection information for a property by searching the type and all its super types
Declaration
public static PropertyInfo Property(string typeColonName)
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The member in the form |
Returns
Type | Description |
---|---|
System.Reflection.PropertyInfo | A property or null when the property cannot be found |
Property(Type, String)
Gets the reflection information for a property by searching the type and all its super types
Declaration
public static PropertyInfo Property(Type type, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type |
System.String | name | The name |
Returns
Type | Description |
---|---|
System.Reflection.PropertyInfo | A property or null when type/name is null or when the property cannot be found |
PropertyGetter(String)
Gets the reflection information for the getter method of a property by searching the type and all its super types
Declaration
public static MethodInfo PropertyGetter(string typeColonName)
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The member in the form |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when type/name is null or when the property cannot be found |
PropertyGetter(Type, String)
Gets the reflection information for the getter method of a property by searching the type and all its super types
Declaration
public static MethodInfo PropertyGetter(Type type, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type |
System.String | name | The name |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when type/name is null or when the property cannot be found |
PropertySetter(String)
Gets the reflection information for the setter method of a property by searching the type and all its super types
Declaration
public static MethodInfo PropertySetter(string typeColonName)
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The member in the form |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when type/name is null or when the property cannot be found |
PropertySetter(Type, String)
Gets the reflection information for the setter method of a property by searching the type and all its super types
Declaration
public static MethodInfo PropertySetter(Type type, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type |
System.String | name | The name |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | A method or null when type/name is null or when the property cannot be found |
RethrowException(Exception)
Rethrows an exception while preserving its stack trace (throw statement typically clobbers existing stack traces)
Declaration
public static void RethrowException(Exception exception)
Parameters
Type | Name | Description |
---|---|---|
System.Exception | exception | The exception to rethrow |
StaticFieldRefAccess<F>(FieldInfo)
Creates a static field reference delegate
Declaration
public static AccessTools.FieldRef<F> StaticFieldRefAccess<F>(FieldInfo fieldInfo)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.FieldInfo | fieldInfo | The field |
Returns
Type | Description |
---|---|
AccessTools.FieldRef<F> | A readable/assignable AccessTools.FieldRef<F> delegate |
Type Parameters
Name | Description |
---|---|
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
StaticFieldRefAccess<F>(String)
Creates a static field reference
Declaration
public static ref F StaticFieldRefAccess<F>(string typeColonName)
Parameters
Type | Name | Description |
---|---|---|
System.String | typeColonName | The member in the form |
Returns
Type | Description |
---|---|
F | A readable/assignable reference to the field |
Type Parameters
Name | Description |
---|---|
F | The type of the field |
StaticFieldRefAccess<F>(Type, String)
Creates a static field reference
Declaration
public static ref F StaticFieldRefAccess<F>(Type type, string fieldName)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type (can be class or struct) the field is defined in |
System.String | fieldName | The name of the field |
Returns
Type | Description |
---|---|
F | A readable/assignable reference to the field |
Type Parameters
Name | Description |
---|---|
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
StaticFieldRefAccess<T, F>(FieldInfo)
Creates a static field reference
Declaration
public static ref F StaticFieldRefAccess<T, F>(FieldInfo fieldInfo)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.FieldInfo | fieldInfo | The field |
Returns
Type | Description |
---|---|
F | A readable/assignable reference to the field |
Type Parameters
Name | Description |
---|---|
T | An arbitrary type (by convention, the type the field is defined in) |
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
Remarks
The type parameter T
is only used in exception messaging and to distinguish between this method overload
and the StaticFieldRefAccess<F>(FieldInfo) overload (which returns a AccessTools.FieldRef<F> rather than a reference).
StaticFieldRefAccess<T, F>(String)
Creates a static field reference
Declaration
public static ref F StaticFieldRefAccess<T, F>(string fieldName)
Parameters
Type | Name | Description |
---|---|---|
System.String | fieldName | The name of the field |
Returns
Type | Description |
---|---|
F | A readable/assignable reference to the field |
Type Parameters
Name | Description |
---|---|
T | The type (can be class or struct) the field is defined in |
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
StructFieldRefAccess<T, F>(ref T, FieldInfo)
Creates a field reference for an instance field of a struct
Declaration
public static ref F StructFieldRefAccess<T, F>(ref T instance, FieldInfo fieldInfo)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
T | instance | The instance |
System.Reflection.FieldInfo | fieldInfo | The field |
Returns
Type | Description |
---|---|
F | A readable/assignable reference to the field |
Type Parameters
Name | Description |
---|---|
T | The struct that defines the instance field |
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
Remarks
This method is meant for one-off access to a field's value for a single instance and where the field has already been obtained.
If you need to access a field's value for potentially multiple instances, use StructFieldRefAccess<T, F>(FieldInfo) instead.
StructFieldRefAccess<T, F>(ref instance, fieldInfo)
is functionally equivalent to StructFieldRefAccess<T, F>(fieldInfo)(ref instance)
.
StructFieldRefAccess<T, F>(ref T, String)
Creates an instance field reference for a specific instance of a struct
Declaration
public static ref F StructFieldRefAccess<T, F>(ref T instance, string fieldName)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
T | instance | The instance |
System.String | fieldName | The name of the field |
Returns
Type | Description |
---|---|
F | A readable/assignable reference to the field |
Type Parameters
Name | Description |
---|---|
T | The struct that defines the instance field |
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
Remarks
This method is meant for one-off access to a field's value for a single instance.
If you need to access a field's value for potentially multiple instances, use StructFieldRefAccess<T, F>(String) instead.
StructFieldRefAccess<T, F>(ref instance, fieldName)
is functionally equivalent to StructFieldRefAccess<T, F>(fieldName)(ref instance)
.
StructFieldRefAccess<T, F>(FieldInfo)
Creates a field reference delegate for an instance field of a struct
Declaration
public static AccessTools.StructFieldRef<T, F> StructFieldRefAccess<T, F>(FieldInfo fieldInfo)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.FieldInfo | fieldInfo | The field |
Returns
Type | Description |
---|---|
AccessTools.StructFieldRef<T, F> | A readable/assignable AccessTools.StructFieldRef<T, F> delegate |
Type Parameters
Name | Description |
---|---|
T | The struct that defines the instance field |
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
Remarks
This method is meant for cases where the field has already been obtained, avoiding the field searching cost in e.g. StructFieldRefAccess<T, F>(String).
StructFieldRefAccess<T, F>(String)
Creates a field reference delegate for an instance field of a struct
Declaration
public static AccessTools.StructFieldRef<T, F> StructFieldRefAccess<T, F>(string fieldName)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
System.String | fieldName | The name of the field |
Returns
Type | Description |
---|---|
AccessTools.StructFieldRef<T, F> | A readable/assignable AccessTools.StructFieldRef<T, F> delegate |
Type Parameters
Name | Description |
---|---|
T | The struct that defines the instance field |
F | The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type |
ThrowMissingMemberException(Type, String[])
Throws a missing member runtime exception
Declaration
public static void ThrowMissingMemberException(Type type, params string[] names)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type that is involved |
System.String[] | names | A list of names |
TypeByName(String)
Gets a type by name. Prefers a full name with namespace but falls back to the first type matching the name otherwise
Declaration
public static Type TypeByName(string name)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name |
Returns
Type | Description |
---|---|
System.Type | A type or null if not found |