Class AccessTools
- Namespace
- HarmonyLib
- Assembly
- 0Harmony.dll
A helper class for reflection related functions
public static class AccessTools
- Inheritance
-
objectAccessTools
Fields
all
Shortcut for System.Reflection.BindingFlags to simplify the use of reflections and make it work for any access level
public static readonly BindingFlags all
Field Value
- 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
public static readonly BindingFlags allDeclared
Field Value
- BindingFlags
Properties
IsMonoRuntime
True if the current runtime is based on Mono, false otherwise (.NET)
public static bool IsMonoRuntime { get; }
Property Value
- bool
IsNetCoreRuntime
True if the current runtime is .NET Core, false otherwise (Mono or .NET Framework)
public static bool IsNetCoreRuntime { get; }
Property Value
- bool
IsNetFrameworkRuntime
True if the current runtime is .NET Framework, false otherwise (.NET Core or Mono, although latter isn't guaranteed)
public static bool IsNetFrameworkRuntime { get; }
Property Value
- bool
Methods
ActualParameters(MethodBase, object[])
Creates an array of input parameters for a given method and a given set of potential inputs
public static object[] ActualParameters(MethodBase method, object[] inputs)
Parameters
methodMethodBaseThe method/constructor you are planing to call
inputsobject[]The possible input parameters in any order
Returns
- object[]
An object array matching the method signature
AllAssemblies()
Enumerates all assemblies in the current app domain, excluding visual studio assemblies
public static IEnumerable<Assembly> AllAssemblies()
Returns
- IEnumerable<Assembly>
An enumeration of System.Reflection.Assembly
AllTypes()
Enumerates all successfully loaded types in the current app domain, excluding visual studio assemblies
public static IEnumerable<Type> AllTypes()
Returns
- IEnumerable<Type>
An enumeration of all System.Type in all assemblies, excluding visual studio assemblies
ClearTypeSearchCache()
Clears the type cache that TypeSearch(Regex, bool) uses
public static void ClearTypeSearchCache()
CombinedHashCode(IEnumerable<object>)
Calculates a combined hash code for an enumeration of objects
public static int CombinedHashCode(IEnumerable<object> objects)
Parameters
objectsIEnumerable<object>The objects
Returns
- int
The hash code
Constructor(Type, Type[], bool)
Gets the reflection information for a constructor by searching the type and all its super types
public static ConstructorInfo Constructor(Type type, Type[] parameters = null, bool searchForStatic = false)
Parameters
typeTypeThe class/type where the constructor is declared
parametersType[]Optional parameters to target a specific overload of the method
searchForStaticboolOptional parameters to only consider static constructors
Returns
- 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
public static object CreateInstance(Type type)
Parameters
typeTypeThe class/type
Returns
- object
The new instance
CreateInstance<T>()
Creates an (possibly uninitialized) instance of a given type
public static T CreateInstance<T>()
Returns
- T
The new instance
Type Parameters
TThe class/type
DeclaredConstructor(Type, Type[], bool)
Gets the reflection information for a directly declared constructor
public static ConstructorInfo DeclaredConstructor(Type type, Type[] parameters = null, bool searchForStatic = false)
Parameters
typeTypeThe class/type where the constructor is declared
parametersType[]Optional parameters to target a specific overload of the constructor
searchForStaticboolOptional parameters to only consider static constructors
Returns
- ConstructorInfo
A constructor info or null when type is null or when the constructor cannot be found
DeclaredEvent(string)
Gets the reflection information for a directly declared event
public static EventInfo DeclaredEvent(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- EventInfo
An event or null when the event cannot be found
DeclaredEvent(Type, string)
Gets the reflection information for a directly declared event
public static EventInfo DeclaredEvent(Type type, string name)
Parameters
typeTypeThe class/type where the event is declared
namestringThe name of the event (case sensitive)
Returns
- EventInfo
An event or null when type/name is null or when the event cannot be found
DeclaredEventAdder(string)
Gets the reflection information for the add method of a directly declared event
public static MethodInfo DeclaredEventAdder(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- MethodInfo
A method or null when the event cannot be found
DeclaredEventAdder(Type, string)
Gets the reflection information for the add method of a directly declared event
public static MethodInfo DeclaredEventAdder(Type type, string name)
Parameters
typeTypeThe class/type where the event is declared
namestringThe name of the event (case sensitive)
Returns
- MethodInfo
A method or null when type/name is null or when the event cannot be found
DeclaredEventRemover(string)
Gets the reflection information for the remove method of a directly declared event
public static MethodInfo DeclaredEventRemover(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- MethodInfo
A method or null when the event cannot be found
DeclaredEventRemover(Type, string)
Gets the reflection information for the remove method of a directly declared event
public static MethodInfo DeclaredEventRemover(Type type, string name)
Parameters
typeTypeThe class/type where the event is declared
namestringThe name of the event (case sensitive)
Returns
- MethodInfo
A method or null when type/name is null or when the event cannot be found
DeclaredField(string)
Gets the reflection information for a directly declared field
public static FieldInfo DeclaredField(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- FieldInfo
A field or null when the field cannot be found
DeclaredField(Type, int)
Gets the reflection information for a field
public static FieldInfo DeclaredField(Type type, int idx)
Parameters
typeTypeThe class/type where the field is declared
idxintThe zero-based index of the field inside the class definition
Returns
- 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
public static FieldInfo DeclaredField(Type type, string name)
Parameters
typeTypeThe class/type where the field is defined
namestringThe name of the field
Returns
- FieldInfo
A field or null when type/name is null or when the field cannot be found
DeclaredFinalizer(Type)
Gets the reflection information for a directly declared finalizer
public static MethodInfo DeclaredFinalizer(Type type)
Parameters
typeTypeThe class/type that defines the finalizer
Returns
- MethodInfo
A method or null when type is null or when the finalizer cannot be found
DeclaredIndexer(Type, Type[])
Gets the reflection information for a directly declared indexer property
public static PropertyInfo DeclaredIndexer(Type type, Type[] parameters = null)
Parameters
typeTypeThe class/type where the indexer property is declared
parametersType[]Optional parameters to target a specific overload of multiple indexers
Returns
- 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
public static MethodInfo DeclaredIndexerGetter(Type type, Type[] parameters = null)
Parameters
typeTypeThe class/type where the indexer property is declared
parametersType[]Optional parameters to target a specific overload of multiple indexers
Returns
- 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
public static MethodInfo DeclaredIndexerSetter(Type type, Type[] parameters)
Parameters
typeTypeThe class/type where the indexer property is declared
parametersType[]Optional parameters to target a specific overload of multiple indexers
Returns
- 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
public static MethodInfo DeclaredMethod(string typeColonName, Type[] parameters = null, Type[] generics = null)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.parametersType[]Optional parameters to target a specific overload of the method
genericsType[]Optional list of types that define the generic version of the method
Returns
- 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
public static MethodInfo DeclaredMethod(Type type, string name, Type[] parameters = null, Type[] generics = null)
Parameters
typeTypeThe class/type where the method is declared
namestringThe name of the method (case sensitive)
parametersType[]Optional parameters to target a specific overload of the method
genericsType[]Optional list of types that define the generic version of the method
Returns
- 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
public static PropertyInfo DeclaredProperty(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- PropertyInfo
A property or null when the property cannot be found
DeclaredProperty(Type, string)
Gets the reflection information for a directly declared property
public static PropertyInfo DeclaredProperty(Type type, string name)
Parameters
typeTypeThe class/type where the property is declared
namestringThe name of the property (case sensitive)
Returns
- 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
public static MethodInfo DeclaredPropertyGetter(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- 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
public static MethodInfo DeclaredPropertyGetter(Type type, string name)
Parameters
typeTypeThe class/type where the property is declared
namestringThe name of the property (case sensitive)
Returns
- 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
public static MethodInfo DeclaredPropertySetter(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- 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
public static MethodInfo DeclaredPropertySetter(Type type, string name)
Parameters
typeTypeThe class/type where the property is declared
namestringThe name of the property (case sensitive)
Returns
- 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
public static MethodInfo EnumeratorMoveNext(MethodBase method)
Parameters
methodMethodBaseEnumerator method that creates the enumerator System.Collections.IEnumerator
Returns
- MethodInfo
The internal System.Collections.IEnumerator.MoveNext() method of the enumerator or null if no valid enumerator is detected
Event(string)
Gets the reflection information for an event by searching the type and all its super types
public static EventInfo Event(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- EventInfo
An event or null when the event cannot be found
Event(Type, string)
Gets the reflection information for an event by searching the type and all its super types
public static EventInfo Event(Type type, string name)
Parameters
typeTypeThe class/type where the event is declared
namestringThe name of the event (case sensitive)
Returns
- EventInfo
An event or null when type/name is null or when the event cannot be found
EventAdder(string)
Gets the reflection information for the add method of an event by searching the type and all its super types
public static MethodInfo EventAdder(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- MethodInfo
A method or null when the event cannot be found
EventAdder(Type, string)
Gets the reflection information for the add method of an event by searching the type and all its super types
public static MethodInfo EventAdder(Type type, string name)
Parameters
typeTypeThe class/type where the event is declared
namestringThe name of the event (case sensitive)
Returns
- MethodInfo
A method or null when type/name is null or when the event cannot be found
EventRemover(string)
Gets the reflection information for the remove method of an event by searching the type and all its super types
public static MethodInfo EventRemover(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- MethodInfo
A method or null when the event cannot be found
EventRemover(Type, string)
Gets the reflection information for the remove method of an event by searching the type and all its super types
public static MethodInfo EventRemover(Type type, string name)
Parameters
typeTypeThe class/type where the event is declared
namestringThe name of the event (case sensitive)
Returns
- MethodInfo
A method or null when type/name is null or when the event cannot be found
Field(string)
Gets the reflection information for a field by searching the type and all its super types
public static FieldInfo Field(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- 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
public static FieldInfo Field(Type type, string name)
Parameters
typeTypeThe class/type where the field is defined
namestringThe name of the field (case sensitive)
Returns
- 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)
public static AccessTools.FieldRef<object, F> FieldRefAccess<F>(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- AccessTools.FieldRef<object, F>
A readable/assignable AccessTools.FieldRef<T, F> delegate with
T=object
Type Parameters
Ftype 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)
public static AccessTools.FieldRef<object, F> FieldRefAccess<F>(Type type, string fieldName)
Parameters
typeTypeThe type that defines the field, or derived class of this type; must not be a struct type unless the field is static
fieldNamestringThe name of the field
Returns
- AccessTools.FieldRef<object, F>
A readable/assignable AccessTools.FieldRef<T, F> delegate with
T=object(for static fields, theinstancedelegate parameter is ignored)
Type Parameters
FThe 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 is assignable from 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>(FieldInfo)
Creates a field reference delegate for an instance field of a class or static field (NOT an instance field of a struct)
public static AccessTools.FieldRef<T, F> FieldRefAccess<T, F>(FieldInfo fieldInfo)
Parameters
fieldInfoFieldInfoThe field
Returns
- AccessTools.FieldRef<T, F>
A readable/assignable AccessTools.FieldRef<T, F> delegate
Type Parameters
TAn arbitrary type if the field is static; otherwise the class that defines the field, or a parent class (including object), implemented interface, or derived class of this type ("
instanceOfT is FieldDeclaringType" must be possible)FThe 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 is assignable from 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
public static AccessTools.FieldRef<T, F> FieldRefAccess<T, F>(string fieldName)
Parameters
fieldNamestringThe name of the field
Returns
- AccessTools.FieldRef<T, F>
A readable/assignable AccessTools.FieldRef<T, F> delegate
Type Parameters
TThe class that defines the instance field, or derived class of this type
FThe 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 is assignable from 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.
FieldRefAccess<T, F>(T, FieldInfo)
Creates a field reference for an instance field of a class
public static ref F FieldRefAccess<T, F>(T instance, FieldInfo fieldInfo)
Parameters
instanceTThe instance
fieldInfoFieldInfoThe field
Returns
- F
A readable/assignable reference to the field
Type Parameters
TThe type that defines the field; or a parent class (including object), implemented interface, or derived class of this type ("
instanceOfT is FieldDeclaringType" must be possible)FThe 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 is assignable from 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
public static ref F FieldRefAccess<T, F>(T instance, string fieldName)
Parameters
instanceTThe instance
fieldNamestringThe name of the field
Returns
- F
A readable/assignable reference to the field
Type Parameters
TThe class that defines the instance field, or derived class of this type
FThe 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 is assignable from 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.
Finalizer(Type)
Gets the reflection information for a finalizer
public static MethodInfo Finalizer(Type type)
Parameters
typeTypeThe class/type that defines the finalizer
Returns
- MethodInfo
A method or null when type is null or when the finalizer cannot be found
FindIncludingBaseTypes<T>(Type, Func<Type, T>)
Applies a function going up the type hierarchy and stops at the first non-null result
public static T FindIncludingBaseTypes<T>(Type type, Func<Type, T> func) where T : class
Parameters
typeTypeThe class/type to start with
funcFunc<Type, T>The evaluation function returning T
Returns
- T
The first non-
nullresult, ornullif no match
Type Parameters
TResult 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 object.
FindIncludingInnerTypes<T>(Type, Func<Type, T>)
Applies a function going into inner types and stops at the first non-null result
public static T FindIncludingInnerTypes<T>(Type type, Func<Type, T> func) where T : class
Parameters
typeTypeThe class/type to start with
funcFunc<Type, T>The evaluation function returning T
Returns
- T
The first non-
nullresult, ornullif no match
Type Parameters
TGeneric type parameter
FirstConstructor(Type, Func<ConstructorInfo, bool>)
Given a type, returns the first constructor matching a predicate
public static ConstructorInfo FirstConstructor(Type type, Func<ConstructorInfo, bool> predicate)
Parameters
typeTypeThe class/type to start searching at
predicateFunc<ConstructorInfo, bool>The predicate to search with
Returns
- 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, bool>)
Given a type, returns the first inner type matching a recursive search with a predicate
public static Type FirstInner(Type type, Func<Type, bool> predicate)
Parameters
typeTypeThe class/type to start searching at
predicateFunc<Type, bool>The predicate to search with
Returns
- 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, bool>)
Given a type, returns the first method matching a predicate
public static MethodInfo FirstMethod(Type type, Func<MethodInfo, bool> predicate)
Parameters
typeTypeThe class/type to start searching at
predicateFunc<MethodInfo, bool>The predicate to search with
Returns
- MethodInfo
The method or null if type/predicate is null or if a type with that name cannot be found
FirstProperty(Type, Func<PropertyInfo, bool>)
Given a type, returns the first property matching a predicate
public static PropertyInfo FirstProperty(Type type, Func<PropertyInfo, bool> predicate)
Parameters
typeTypeThe class/type to start searching at
predicateFunc<PropertyInfo, bool>The predicate to search with
Returns
- PropertyInfo
The property or null if type/predicate is null or if a type with that name cannot be found
GetDeclaredConstructors(Type, bool?)
Gets reflection information for all declared constructors
public static List<ConstructorInfo> GetDeclaredConstructors(Type type, bool? searchForStatic = null)
Parameters
typeTypeThe class/type where the constructors are declared
searchForStaticbool?Optional parameters to only consider static constructors
Returns
- List<ConstructorInfo>
A list of constructor infos
GetDeclaredFields(Type)
Gets reflection information for all declared fields
public static List<FieldInfo> GetDeclaredFields(Type type)
Parameters
typeTypeThe class/type where the fields are declared
Returns
- List<FieldInfo>
A list of fields
GetDeclaredMember<T>(T)
Gets the real implementation of a class member
public static T GetDeclaredMember<T>(this T member) where T : MemberInfo
Parameters
memberTA member
Returns
- T
The member itself if its declared. Otherwise the member that is actually implemented in some base type
Type Parameters
T
GetDeclaredMethods(Type)
Gets reflection information for all declared methods
public static List<MethodInfo> GetDeclaredMethods(Type type)
Parameters
typeTypeThe class/type where the methods are declared
Returns
- List<MethodInfo>
A list of methods
GetDeclaredProperties(Type)
Gets reflection information for all declared properties
public static List<PropertyInfo> GetDeclaredProperties(Type type)
Parameters
typeTypeThe class/type where the properties are declared
Returns
- List<PropertyInfo>
A list of properties
GetDefaultValue(Type)
Gets default value for a specific type
public static object GetDefaultValue(Type type)
Parameters
typeTypeThe class/type
Returns
- object
The default value
GetFieldNames(object)
Gets the names of all fields that are declared in the type of the instance
public static List<string> GetFieldNames(object instance)
Parameters
instanceobjectAn instance of the type to search in
Returns
- List<string>
A list of field names
GetFieldNames(Type)
Gets the names of all fields that are declared in a type
public static List<string> GetFieldNames(Type type)
Parameters
typeTypeThe declaring class/type
Returns
- List<string>
A list of field names
GetMethodByModuleAndToken(string, int)
Returns a System.Reflection.MethodInfo by searching for module-id and token
public static MethodInfo GetMethodByModuleAndToken(string moduleGUID, int token)
Parameters
moduleGUIDstringThe module of the method
tokenintThe token of the method
Returns
- MethodInfo
GetMethodNames(object)
Gets the names of all method that are declared in the type of the instance
public static List<string> GetMethodNames(object instance)
Parameters
instanceobjectAn instance of the type to search in
Returns
- List<string>
A list of method names
GetMethodNames(Type)
Gets the names of all method that are declared in a type
public static List<string> GetMethodNames(Type type)
Parameters
typeTypeThe declaring class/type
Returns
- List<string>
A list of method names
GetOutsideCaller()
Returns who called the current method
public static MethodBase GetOutsideCaller()
Returns
- 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
public static List<string> GetPropertyNames(object instance)
Parameters
instanceobjectAn instance of the type to search in
Returns
- List<string>
A list of property names
GetPropertyNames(Type)
Gets the names of all properties that are declared in a type
public static List<string> GetPropertyNames(Type type)
Parameters
typeTypeThe declaring class/type
Returns
- List<string>
A list of property names
GetReturnedType(MethodBase)
Gets the return type of a method or constructor
public static Type GetReturnedType(MethodBase methodOrConstructor)
Parameters
methodOrConstructorMethodBaseThe method/constructor
Returns
- Type
The return type
GetTypes(object[])
Returns an array containing the type of each object in the given array
public static Type[] GetTypes(object[] parameters)
Parameters
parametersobject[]An array of objects
Returns
- 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
public static Type[] GetTypesFromAssembly(Assembly assembly)
Parameters
assemblyAssemblyThe assembly
Returns
- 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
public static Type GetUnderlyingType(this MemberInfo member)
Parameters
memberMemberInfoA member
Returns
- Type
The class/type of this member
HarmonyDelegate<DelegateType>(object)
Creates a delegate for a given delegate definition, attributed with [HarmonyDelegate]
public static DelegateType HarmonyDelegate<DelegateType>(object instance = null) where DelegateType : Delegate
Parameters
instanceobjectOnly applies for instance methods. If
null(default), returned delegate is an open (a.k.a. unbound) instance delegate where an instance is supplied as the first argument to the delegate invocation; else, delegate is a closed (a.k.a. bound) instance delegate where the delegate invocation always applies to the giveninstance.
Returns
- DelegateType
A delegate of given
DelegateTypeto the method specified via [HarmonyDelegate] attributes onDelegateType
Type Parameters
DelegateTypeThe delegate Type, attributed with [HarmonyDelegate]
Remarks
This calls MethodDelegate<DelegateType>(MethodInfo, object, bool, Type[]) 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
public static MethodInfo Identifiable(this MethodInfo method)
Parameters
methodMethodInfoThe method
Returns
- MethodInfo
Indexer(Type, Type[])
Gets the reflection information for an indexer property by searching the type and all its super types
public static PropertyInfo Indexer(Type type, Type[] parameters = null)
Parameters
typeTypeThe class/type
parametersType[]Optional parameters to target a specific overload of multiple indexers
Returns
- 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
public static MethodInfo IndexerGetter(Type type, Type[] parameters = null)
Parameters
typeTypeThe class/type
parametersType[]Optional parameters to target a specific overload of multiple indexers
Returns
- 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
public static MethodInfo IndexerSetter(Type type, Type[] parameters = null)
Parameters
typeTypeThe class/type
parametersType[]Optional parameters to target a specific overload of multiple indexers
Returns
- 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
public static Type Inner(Type type, string name)
Parameters
typeTypeThe class/type to start searching at
namestringThe name of the inner type (case sensitive)
Returns
- 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
public static IEnumerable<Type> InnerTypes(Type type)
Parameters
typeTypeThe class/type to start with
Returns
- IEnumerable<Type>
An enumeration of all inner System.Type
IsClass(Type)
Tests if a type is a class
public static bool IsClass(Type type)
Parameters
typeTypeThe type
Returns
- bool
True if the type is a class
IsDeclaredMember<T>(T)
Test if a class member is actually an concrete implementation
public static bool IsDeclaredMember<T>(this T member) where T : MemberInfo
Parameters
memberTA member
Returns
- bool
True if the member is a declared
Type Parameters
T
IsFloatingPoint(Type)
Tests if a type is a floating point type
public static bool IsFloatingPoint(Type type)
Parameters
typeTypeThe type
Returns
- bool
True if the type represents some floating point
IsInteger(Type)
Tests if a type is an integer type
public static bool IsInteger(Type type)
Parameters
typeTypeThe type
Returns
- bool
True if the type represents some integer
IsNumber(Type)
Tests if a type is a numerical type
public static bool IsNumber(Type type)
Parameters
typeTypeThe type
Returns
- bool
True if the type represents some number
IsOfNullableType<T>(T)
Test whether an instance is of a nullable type
public static bool IsOfNullableType<T>(T instance)
Parameters
instanceTAn instance to test
Returns
- bool
True if instance is of nullable type, false if not
Type Parameters
TType of instance
IsStatic(MemberInfo)
Tests whether a type or member is static, as defined in C#
public static bool IsStatic(MemberInfo member)
Parameters
memberMemberInfoThe type or member
Returns
- bool
True if the type or member is static
IsStruct(Type)
Tests if a type is a struct
public static bool IsStruct(Type type)
Parameters
typeTypeThe type
Returns
- bool
True if the type is a struct
IsValue(Type)
Tests if a type is a value type
public static bool IsValue(Type type)
Parameters
typeTypeThe type
Returns
- bool
True if the type is a value type
IsVoid(Type)
Tests if a type is void
public static bool IsVoid(Type type)
Parameters
typeTypeThe type
Returns
- bool
True if the type is void
Lambdas(MethodBase)
Finds lambda implementation methods referenced directly by an exact containing method
public static IEnumerable<MethodInfo> Lambdas(MethodBase containingMethod)
Parameters
containingMethodMethodBaseThe source method, or a resolved generated method
Returns
- IEnumerable<MethodInfo>
Distinct lambda methods in deterministic metadata order
Remarks
The order can change after recompilation. Compiler-generated state-machine bodies are inspected for iterator and async methods.
LocalFunction(MethodBase, string, Type[])
Finds a compiler-generated local function directly referenced by an exact containing method
public static MethodInfo LocalFunction(MethodBase containingMethod, string name, Type[] parameters = null)
Parameters
containingMethodMethodBaseThe source method, or a resolved local function for nested-local lookup
namestringThe local function's source name
parametersType[]Optional source parameter types, excluding compiler-supplied closure arguments
Returns
- MethodInfo
The selected generated method
Exceptions
- ArgumentException
The local function was not preserved as a referenced generated method
- AmbiguousMatchException
Several referenced local functions match
MakeDeepCopy(object, Type, Func<string, Traverse, Traverse, object>, string)
Makes a deep copy of any object
public static object MakeDeepCopy(object source, Type resultType, Func<string, Traverse, Traverse, object> processor = null, string pathRoot = "")
Parameters
sourceobjectThe original object
resultTypeTypeThe type of the instance that should be created
processorFunc<string, Traverse, Traverse, object>Optional value transformation function (taking a field name and src/dst Traverse instances)
pathRootstringThe optional path root to start with
Returns
- object
The copy of the original object
MakeDeepCopy<T>(object)
Makes a deep copy of any object
public static T MakeDeepCopy<T>(object source) where T : class
Parameters
sourceobjectThe original object
Returns
- T
A copy of the original object but of type T
Type Parameters
TThe 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
public static void MakeDeepCopy<T>(object source, out T result, Func<string, Traverse, Traverse, object> processor = null, string pathRoot = "")
Parameters
sourceobjectThe original object
resultT[out] The copy of the original object
processorFunc<string, Traverse, Traverse, object>Optional value transformation function (taking a field name and src/dst Traverse instances)
pathRootstringThe optional path root to start with
Type Parameters
TThe 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
public static MethodInfo Method(string typeColonName, Type[] parameters = null, Type[] generics = null)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.parametersType[]Optional parameters to target a specific overload of the method
genericsType[]Optional list of types that define the generic version of the method
Returns
- 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
public static MethodInfo Method(Type type, string name, Type[] parameters = null, Type[] generics = null)
Parameters
typeTypeThe class/type where the method is declared
namestringThe name of the method (case sensitive)
parametersType[]Optional parameters to target a specific overload of the method
genericsType[]Optional list of types that define the generic version of the method
Returns
- MethodInfo
A method or null when type/name is null or when the method cannot be found
MethodDelegate<DelegateType>(MethodInfo, object, bool, Type[])
Creates a delegate to a given method
public static DelegateType MethodDelegate<DelegateType>(MethodInfo method, object instance = null, bool virtualCall = true, Type[] delegateArgs = null) where DelegateType : Delegate
Parameters
methodMethodInfoThe method to create a delegate from.
instanceobjectOnly applies for instance methods. If
null(default), returned delegate is an open (a.k.a. unbound) instance delegate where an instance is supplied as the first argument to the delegate invocation; else, delegate is a closed (a.k.a. bound) instance delegate where the delegate invocation always applies to the giveninstance.virtualCallboolOnly applies for instance methods. If
true(default) andmethodis virtual, invocation of the delegate calls the instance method virtually (the instance type's most-derived/overriden implementation of the method is called); else, invocation of the delegate calls the exact specifiedmethod(this is useful for calling base class methods) Note: iffalseandmethodis an interface method, an ArgumentException is thrown.delegateArgsType[]Only applies for instance methods, and if argument
instanceis null. This argument only matters if the targetmethodsignature contains a value type (such as struct or primitive types), and yourDelegateTypeargument is replaced by a non-value type (usuallyobject) instead of using said value type. Use this if the generic arguments ofDelegateTypedoesn't represent the delegate's arguments, and calling this function failsA delegate of given DelegateTypeto givenmethod
Returns
- DelegateType
Type Parameters
DelegateTypeThe delegate Type
Remarks
Delegate invocation is more performant and more convenient to use than System.Reflection.MethodBase.Invoke(object, 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, bool, Type[])
Creates a delegate to a given method
public static DelegateType MethodDelegate<DelegateType>(string typeColonName, object instance = null, bool virtualCall = true, Type[] delegateArgs = null) where DelegateType : Delegate
Parameters
typeColonNamestringThe method in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.instanceobjectOnly applies for instance methods. If
null(default), returned delegate is an open (a.k.a. unbound) instance delegate where an instance is supplied as the first argument to the delegate invocation; else, delegate is a closed (a.k.a. bound) instance delegate where the delegate invocation always applies to the giveninstance.virtualCallboolOnly applies for instance methods. If
true(default) andtypeColonNameis virtual, invocation of the delegate calls the instance method virtually (the instance type's most-derived/overriden implementation of the method is called); else, invocation of the delegate calls the exact specifiedtypeColonName(this is useful for calling base class methods) Note: iffalseandtypeColonNameis an interface method, an ArgumentException is thrown.delegateArgsType[]Only applies for instance methods, and if argument
instanceis null. This argument only matters if the targettypeColonNamesignature contains a value type (such as struct or primitive types), and yourDelegateTypeargument is replaced by a non-value type (usuallyobject) instead of using said value type. Use this if the generic arguments ofDelegateTypedoesn't represent the delegate's arguments, and calling this function failsA delegate of given DelegateTypeto giventypeColonName
Returns
- DelegateType
Type Parameters
DelegateTypeThe delegate Type
Remarks
Delegate invocation is more performant and more convenient to use than System.Reflection.MethodBase.Invoke(object, 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
public static PropertyInfo Property(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- 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
public static PropertyInfo Property(Type type, string name)
Parameters
typeTypeThe class/type
namestringThe name
Returns
- 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
public static MethodInfo PropertyGetter(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- 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
public static MethodInfo PropertyGetter(Type type, string name)
Parameters
typeTypeThe class/type
namestringThe name
Returns
- 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
public static MethodInfo PropertySetter(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- 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
public static MethodInfo PropertySetter(Type type, string name)
Parameters
typeTypeThe class/type
namestringThe name
Returns
- 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)
public static void RethrowException(Exception exception)
Parameters
exceptionExceptionThe exception to rethrow
StateMachineMoveNext(MethodBase)
Resolves the execution body of a compiler-generated iterator, async method, or async iterator
public static MethodInfo StateMachineMoveNext(MethodBase method)
Parameters
methodMethodBaseThe declared method, or its already resolved execution body
Returns
- MethodInfo
The generated MoveNext method, the supplied method if it is already that body, or null for an ordinary method
Remarks
Async iterators resolve to IAsyncStateMachine.MoveNext, not MoveNextAsync. Open generic methods must be closed first.
Exceptions
- ArgumentException
Recognized state-machine metadata is malformed or cannot be closed from the supplied method
StaticFieldRefAccess<F>(FieldInfo)
Creates a static field reference delegate
public static AccessTools.FieldRef<F> StaticFieldRefAccess<F>(FieldInfo fieldInfo)
Parameters
fieldInfoFieldInfoThe field
Returns
- AccessTools.FieldRef<F>
A readable/assignable AccessTools.FieldRef<F> delegate
Type Parameters
FThe 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 is assignable from 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
public static ref F StaticFieldRefAccess<F>(string typeColonName)
Parameters
typeColonNamestringThe member in the form
TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType likeSome.Namespace.Type.
Returns
- F
A readable/assignable reference to the field
Type Parameters
FThe type of the field
StaticFieldRefAccess<F>(Type, string)
Creates a static field reference
public static ref F StaticFieldRefAccess<F>(Type type, string fieldName)
Parameters
typeTypeThe type (can be class or struct) the field is defined in
fieldNamestringThe name of the field
Returns
- F
A readable/assignable reference to the field
Type Parameters
FThe 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 is assignable from 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
public static ref F StaticFieldRefAccess<T, F>(FieldInfo fieldInfo)
Parameters
fieldInfoFieldInfoThe field
Returns
- F
A readable/assignable reference to the field
Type Parameters
TAn arbitrary type (by convention, the type the field is defined in)
FThe 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 is assignable from 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
public static ref F StaticFieldRefAccess<T, F>(string fieldName)
Parameters
fieldNamestringThe name of the field
Returns
- F
A readable/assignable reference to the field
Type Parameters
TThe type (can be class or struct) the field is defined in
FThe 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 is assignable from 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>(FieldInfo)
Creates a field reference delegate for an instance field of a struct
public static AccessTools.StructFieldRef<T, F> StructFieldRefAccess<T, F>(FieldInfo fieldInfo) where T : struct
Parameters
fieldInfoFieldInfoThe field
Returns
- AccessTools.StructFieldRef<T, F>
A readable/assignable AccessTools.StructFieldRef<T, F> delegate
Type Parameters
TThe struct that defines the instance field
FThe 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 is assignable from 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
public static AccessTools.StructFieldRef<T, F> StructFieldRefAccess<T, F>(string fieldName) where T : struct
Parameters
fieldNamestringThe name of the field
Returns
- AccessTools.StructFieldRef<T, F>
A readable/assignable AccessTools.StructFieldRef<T, F> delegate
Type Parameters
TThe struct that defines the instance field
FThe 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 is assignable from 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
public static ref F StructFieldRefAccess<T, F>(ref T instance, FieldInfo fieldInfo) where T : struct
Parameters
instanceTThe instance
fieldInfoFieldInfoThe field
Returns
- F
A readable/assignable reference to the field
Type Parameters
TThe struct that defines the instance field
FThe 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 is assignable from 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
public static ref F StructFieldRefAccess<T, F>(ref T instance, string fieldName) where T : struct
Parameters
instanceTThe instance
fieldNamestringThe name of the field
Returns
- F
A readable/assignable reference to the field
Type Parameters
TThe struct that defines the instance field
FThe 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 is assignable from 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).
ThrowMissingMemberException(Type, params string[])
Throws a missing member runtime exception
public static void ThrowMissingMemberException(Type type, params string[] names)
Parameters
typeTypeThe type that is involved
namesstring[]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
public static Type TypeByName(string name)
Parameters
namestringThe name
Returns
- Type
A type or null if not found
TypeSearch(Regex, bool)
Searches a type by regular expression; for exact searching, use TypeByName(string)
public static Type TypeSearch(Regex search, bool invalidateCache = false)
Parameters
searchRegexThe regular expression that matches against Type.FullName or Type.Name
invalidateCacheboolRefetches the cached types if set to true
Returns
- Type
The first type where FullName or Name matches the search