Show / Hide Table of Contents

Class AccessToolsExtensions

Adds extensions to Type for a lot of AccessTools methods

Inheritance
object
AccessToolsExtensions
Namespace: HarmonyLib
Assembly: 0Harmony.dll
Syntax
public static class AccessToolsExtensions

Methods

Constructor(Type, Type[], bool)

Gets the reflection information for a constructor by searching the type and all its super types

Declaration
public static ConstructorInfo Constructor(this Type type, Type[] parameters = null, bool searchForStatic = false)
Parameters
Type Name Description
Type type

The class/type where the constructor is declared

Type[] parameters

Optional parameters to target a specific overload of the method

bool searchForStatic

Optional parameters to only consider static constructors

Returns
Type Description
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(this Type type)
Parameters
Type Name Description
Type type

The class/type

Returns
Type Description
object

The new instance

DeclaredConstructor(Type, Type[], bool)

Gets the reflection information for a directly declared constructor

Declaration
public static ConstructorInfo DeclaredConstructor(this Type type, Type[] parameters = null, bool searchForStatic = false)
Parameters
Type Name Description
Type type

The class/type where the constructor is declared

Type[] parameters

Optional parameters to target a specific overload of the constructor

bool searchForStatic

Optional parameters to only consider static constructors

Returns
Type Description
ConstructorInfo

A constructor info or null when type is null or when the constructor cannot be found

DeclaredEvent(Type, string)

Gets the reflection information for a directly declared event

Declaration
public static EventInfo DeclaredEvent(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type where the event is declared

string name

The name of the event (case sensitive)

Returns
Type Description
EventInfo

An event or null when type/name is null or when the event cannot be found

DeclaredEventAdder(Type, string)

Gets the reflection information for the add method of a directly declared event

Declaration
public static MethodInfo DeclaredEventAdder(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type where the event is declared

string name

The name of the event (case sensitive)

Returns
Type Description
MethodInfo

A method or null when type/name is null or when the event cannot be found

DeclaredEventRemover(Type, string)

Gets the reflection information for the remove method of a directly declared event

Declaration
public static MethodInfo DeclaredEventRemover(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type where the event is declared

string name

The name of the event (case sensitive)

Returns
Type Description
MethodInfo

A method or null when type/name is null or when the event cannot be found

DeclaredField(Type, int)

Gets the reflection information for a field

Declaration
public static FieldInfo DeclaredField(this Type type, int idx)
Parameters
Type Name Description
Type type

The class/type where the field is declared

int idx

The zero-based index of the field inside the class definition

Returns
Type Description
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(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type where the field is defined

string name

The name of the field

Returns
Type Description
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

Declaration
public static MethodInfo DeclaredFinalizer(this Type type)
Parameters
Type Name Description
Type type

The class/type that defines the finalizer

Returns
Type Description
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

Declaration
public static PropertyInfo DeclaredIndexer(this Type type, Type[] parameters = null)
Parameters
Type Name Description
Type type

The class/type where the indexer property is declared

Type[] parameters

Optional parameters to target a specific overload of multiple indexers

Returns
Type Description
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(this Type type, Type[] parameters = null)
Parameters
Type Name Description
Type type

The class/type where the indexer property is declared

Type[] parameters

Optional parameters to target a specific overload of multiple indexers

Returns
Type Description
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(this Type type, Type[] parameters)
Parameters
Type Name Description
Type type

The class/type where the indexer property is declared

Type[] parameters

Optional parameters to target a specific overload of multiple indexers

Returns
Type Description
MethodInfo

A method or null when type is null or when indexer property cannot be found

DeclaredMethod(Type, string, Type[], Type[])

Gets the reflection information for a directly declared method

Declaration
public static MethodInfo DeclaredMethod(this Type type, string name, Type[] parameters = null, Type[] generics = null)
Parameters
Type Name Description
Type type

The class/type where the method is declared

string name

The name of the method (case sensitive)

Type[] parameters

Optional parameters to target a specific overload of the method

Type[] generics

Optional list of types that define the generic version of the method

Returns
Type Description
MethodInfo

A method or null when type/name is null or when the method cannot be found

DeclaredProperty(Type, string)

Gets the reflection information for a directly declared property

Declaration
public static PropertyInfo DeclaredProperty(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type where the property is declared

string name

The name of the property (case sensitive)

Returns
Type Description
PropertyInfo

A property or null when type/name is null or 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(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type where the property is declared

string name

The name of the property (case sensitive)

Returns
Type Description
MethodInfo

A method or null when type/name is null or 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(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type where the property is declared

string name

The name of the property (case sensitive)

Returns
Type Description
MethodInfo

A method or null when type/name is null or when the property cannot be found

Event(Type, string)

Gets the reflection information for an event by searching the type and all its super types

Declaration
public static EventInfo Event(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type

string name

The name

Returns
Type Description
EventInfo

An event or null when type/name is null or 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

Declaration
public static MethodInfo EventAdder(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type

string name

The name

Returns
Type Description
MethodInfo

A method or null when type/name is null or 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

Declaration
public static MethodInfo EventRemover(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type

string name

The name

Returns
Type Description
MethodInfo

A method or null when type/name is null or when the event 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(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type where the field is defined

string name

The name of the field (case sensitive)

Returns
Type Description
FieldInfo

A field or null when type/name is null or when the field cannot be found

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>(this Type type, string fieldName)
Parameters
Type Name Description
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

string fieldName

The name of the field

Returns
Type Description
AccessTools.FieldRef<object, F>

A readable/assignable AccessTools.FieldRef<T, F> delegate with T=object (for static fields, the instance delegate parameter is ignored)

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 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.

Finalizer(Type)

Gets the reflection information for a finalizer

Declaration
public static MethodInfo Finalizer(this Type type)
Parameters
Type Name Description
Type type

The class/type that defines the finalizer

Returns
Type Description
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

Declaration
public static T FindIncludingBaseTypes<T>(this Type type, Func<Type, T> func) where T : class
Parameters
Type Name Description
Type type

The class/type to start with

Func<Type, T> func

The evaluation function returning T

Returns
Type Description
T

The first non-null result, or null if no match

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 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>(this Type type, Func<Type, T> func) where T : class
Parameters
Type Name Description
Type type

The class/type to start with

Func<Type, T> func

The evaluation function returning T

Returns
Type Description
T

The first non-null result, or null if no match

Type Parameters
Name Description
T

Generic type parameter

FirstConstructor(Type, Func<ConstructorInfo, bool>)

Given a type, returns the first constructor matching a predicate

Declaration
public static ConstructorInfo FirstConstructor(this Type type, Func<ConstructorInfo, bool> predicate)
Parameters
Type Name Description
Type type

The class/type to start searching at

Func<ConstructorInfo, bool> predicate

The predicate to search with

Returns
Type Description
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

Declaration
public static Type FirstInner(this Type type, Func<Type, bool> predicate)
Parameters
Type Name Description
Type type

The class/type to start searching at

Func<Type, bool> predicate

The predicate to search with

Returns
Type Description
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

Declaration
public static MethodInfo FirstMethod(this Type type, Func<MethodInfo, bool> predicate)
Parameters
Type Name Description
Type type

The class/type to start searching at

Func<MethodInfo, bool> predicate

The predicate to search with

Returns
Type Description
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

Declaration
public static PropertyInfo FirstProperty(this Type type, Func<PropertyInfo, bool> predicate)
Parameters
Type Name Description
Type type

The class/type to start searching at

Func<PropertyInfo, bool> predicate

The predicate to search with

Returns
Type Description
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

Declaration
public static List<ConstructorInfo> GetDeclaredConstructors(this Type type, bool? searchForStatic = null)
Parameters
Type Name Description
Type type

The class/type where the constructors are declared

bool? searchForStatic

Optional parameters to only consider static constructors

Returns
Type Description
List<ConstructorInfo>

A list of constructor infos

GetDeclaredFields(Type)

Gets reflection information for all declared fields

Declaration
public static List<FieldInfo> GetDeclaredFields(this Type type)
Parameters
Type Name Description
Type type

The class/type where the fields are declared

Returns
Type Description
List<FieldInfo>

A list of fields

GetDeclaredMethods(Type)

Gets reflection information for all declared methods

Declaration
public static List<MethodInfo> GetDeclaredMethods(this Type type)
Parameters
Type Name Description
Type type

The class/type where the methods are declared

Returns
Type Description
List<MethodInfo>

A list of methods

GetDeclaredProperties(Type)

Gets reflection information for all declared properties

Declaration
public static List<PropertyInfo> GetDeclaredProperties(this Type type)
Parameters
Type Name Description
Type type

The class/type where the properties are declared

Returns
Type Description
List<PropertyInfo>

A list of properties

GetDefaultValue(Type)

Gets default value for a specific type

Declaration
public static object GetDefaultValue(this Type type)
Parameters
Type Name Description
Type type

The class/type

Returns
Type Description
object

The default value

GetFieldNames(Type)

Gets the names of all fields that are declared in a type

Declaration
public static List<string> GetFieldNames(this Type type)
Parameters
Type Name Description
Type type

The declaring class/type

Returns
Type Description
List<string>

A list of field names

GetMethodNames(Type)

Gets the names of all method that are declared in a type

Declaration
public static List<string> GetMethodNames(this Type type)
Parameters
Type Name Description
Type type

The declaring class/type

Returns
Type Description
List<string>

A list of method names

GetPropertyNames(Type)

Gets the names of all properties that are declared in a type

Declaration
public static List<string> GetPropertyNames(this Type type)
Parameters
Type Name Description
Type type

The declaring class/type

Returns
Type Description
List<string>

A list of property names

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(this Type type, Type[] parameters = null)
Parameters
Type Name Description
Type type

The class/type

Type[] parameters

Optional parameters to target a specific overload of multiple indexers

Returns
Type Description
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(this Type type, Type[] parameters = null)
Parameters
Type Name Description
Type type

The class/type

Type[] parameters

Optional parameters to target a specific overload of multiple indexers

Returns
Type Description
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(this Type type, Type[] parameters = null)
Parameters
Type Name Description
Type type

The class/type

Type[] parameters

Optional parameters to target a specific overload of multiple indexers

Returns
Type Description
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(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type to start searching at

string name

The name of the inner type (case sensitive)

Returns
Type Description
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)

Declaration
public static IEnumerable<Type> InnerTypes(this Type type)
Parameters
Type Name Description
Type type

The class/type to start with

Returns
Type Description
IEnumerable<Type>

An enumeration of all inner System.Type

IsClass(Type)

Tests if a type is a class

Declaration
public static bool IsClass(this Type type)
Parameters
Type Name Description
Type type

The type

Returns
Type Description
bool

True if the type is a class

IsFloatingPoint(Type)

Tests if a type is a floating point type

Declaration
public static bool IsFloatingPoint(this Type type)
Parameters
Type Name Description
Type type

The type

Returns
Type Description
bool

True if the type represents some floating point

IsInteger(Type)

Tests if a type is an integer type

Declaration
public static bool IsInteger(this Type type)
Parameters
Type Name Description
Type type

The type

Returns
Type Description
bool

True if the type represents some integer

IsNumber(Type)

Tests if a type is a numerical type

Declaration
public static bool IsNumber(this Type type)
Parameters
Type Name Description
Type type

The type

Returns
Type Description
bool

True if the type represents some number

IsStruct(Type)

Tests if a type is a struct

Declaration
public static bool IsStruct(this Type type)
Parameters
Type Name Description
Type type

The type

Returns
Type Description
bool

True if the type is a struct

IsValue(Type)

Tests if a type is a value type

Declaration
public static bool IsValue(this Type type)
Parameters
Type Name Description
Type type

The type

Returns
Type Description
bool

True if the type is a value type

IsVoid(Type)

Tests if a type is void

Declaration
public static bool IsVoid(this Type type)
Parameters
Type Name Description
Type type

The type

Returns
Type Description
bool

True if the type is void

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(this Type type, string name, Type[] parameters = null, Type[] generics = null)
Parameters
Type Name Description
Type type

The class/type where the method is declared

string name

The name of the method (case sensitive)

Type[] parameters

Optional parameters to target a specific overload of the method

Type[] generics

Optional list of types that define the generic version of the method

Returns
Type Description
MethodInfo

A method or null when type/name is null or when the method 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(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type

string name

The name

Returns
Type Description
PropertyInfo

A property 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(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type

string name

The name

Returns
Type Description
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(this Type type, string name)
Parameters
Type Name Description
Type type

The class/type

string name

The name

Returns
Type Description
MethodInfo

A method or null when type/name is null or when the property cannot be found

StaticFieldRefAccess<F>(Type, string)

Creates a static field reference

Declaration
public static ref F StaticFieldRefAccess<F>(this Type type, string fieldName)
Parameters
Type Name Description
Type type

The type (can be class or struct) the field is defined in

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

ThrowMissingMemberException(Type, params string[])

Throws a missing member runtime exception

Declaration
public static void ThrowMissingMemberException(this Type type, params string[] names)
Parameters
Type Name Description
Type type

The type that is involved

string[] names

A list of names

In this article
Back to top Generated by DocFX