Back to Harmony

Class AccessToolsExtensions

docs/api/HarmonyLib.AccessToolsExtensions.html

2.4.2.031.4 KB
Original Source

Class AccessToolsExtensions

Adds extensions to Type for a lot of AccessTools methods

Inheritance

System.Object

AccessToolsExtensions

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

Methods

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(this Type type, Type[] parameters = null, bool searchForStatic = false)
Parameters
TypeNameDescription
System.Typetype

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
TypeDescription
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(this Type type)
Parameters
TypeNameDescription
System.Typetype

The class/type

|

Returns
TypeDescription
System.Object

The new instance

|

DeclaredConstructor(Type, Type[], Boolean)

Gets the reflection information for a directly declared constructor

Declaration
public static ConstructorInfo DeclaredConstructor(this Type type, Type[] parameters = null, bool searchForStatic = false)
Parameters
TypeNameDescription
System.Typetype

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
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type where the event is declared

| | System.String | name |

The name of the event (case sensitive)

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type where the event is declared

| | System.String | name |

The name of the event (case sensitive)

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type where the event is declared

| | System.String | name |

The name of the event (case sensitive)

|

Returns
TypeDescription
System.Reflection.MethodInfo

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

|

DeclaredField(Type, Int32)

Gets the reflection information for a field

Declaration
public static FieldInfo DeclaredField(this Type type, int idx)
Parameters
TypeNameDescription
System.Typetype

The class/type where the field is declared

| | System.Int32 | idx |

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

|

Returns
TypeDescription
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(this Type type, string name)
Parameters
TypeNameDescription
System.Typetype

The class/type where the field is defined

| | System.String | name |

The name of the field

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type that defines the finalizer

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type where the indexer property is declared

| | System.Type[] | parameters |

Optional parameters to target a specific overload of multiple indexers

|

Returns
TypeDescription
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(this Type type, Type[] parameters = null)
Parameters
TypeNameDescription
System.Typetype

The class/type where the indexer property is declared

| | System.Type[] | parameters |

Optional parameters to target a specific overload of multiple indexers

|

Returns
TypeDescription
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(this Type type, Type[] parameters)
Parameters
TypeNameDescription
System.Typetype

The class/type where the indexer property is declared

| | System.Type[] | parameters |

Optional parameters to target a specific overload of multiple indexers

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

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
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type where the property is declared

| | System.String | name |

The name of the property (case sensitive)

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type where the property is declared

| | System.String | name |

The name of the property (case sensitive)

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type where the property is declared

| | System.String | name |

The name of the property (case sensitive)

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type

| | System.String | name |

The name

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type

| | System.String | name |

The name

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type

| | System.String | name |

The name

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type where the field is defined

| | System.String | name |

The name of the field (case sensitive)

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

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
TypeDescription
AccessTools.FieldRef\

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

|

Type Parameters
NameDescription
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.

Finalizer(Type)

Gets the reflection information for a finalizer

Declaration
public static MethodInfo Finalizer(this Type type)
Parameters
TypeNameDescription
System.Typetype

The class/type that defines the finalizer

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type to start with

| | System.Func\ | func |

The evaluation function returning T

|

Returns
TypeDescription
T

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

|

Type Parameters
NameDescription
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>(this Type type, Func<Type, T> func)
    where T : class
Parameters
TypeNameDescription
System.Typetype

The class/type to start with

| | System.Func\ | func |

The evaluation function returning T

|

Returns
TypeDescription
T

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

|

Type Parameters
NameDescription
T

Generic type parameter

|

FirstConstructor(Type, Func<ConstructorInfo, Boolean>)

Given a type, returns the first constructor matching a predicate

Declaration
public static ConstructorInfo FirstConstructor(this Type type, Func<ConstructorInfo, bool> predicate)
Parameters
TypeNameDescription
System.Typetype

The class/type to start searching at

| | System.Func\ | predicate |

The predicate to search with

|

Returns
TypeDescription
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(this Type type, Func<Type, bool> predicate)
Parameters
TypeNameDescription
System.Typetype

The class/type to start searching at

| | System.Func\ | predicate |

The predicate to search with

|

Returns
TypeDescription
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(this Type type, Func<MethodInfo, bool> predicate)
Parameters
TypeNameDescription
System.Typetype

The class/type to start searching at

| | System.Func\ | predicate |

The predicate to search with

|

Returns
TypeDescription
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(this Type type, Func<PropertyInfo, bool> predicate)
Parameters
TypeNameDescription
System.Typetype

The class/type to start searching at

| | System.Func\ | predicate |

The predicate to search with

|

Returns
TypeDescription
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(this Type type, bool? searchForStatic = default(bool? ))
Parameters
TypeNameDescription
System.Typetype

The class/type where the constructors are declared

| | System.Nullable\ | searchForStatic |

Optional parameters to only consider static constructors

|

Returns
TypeDescription
System.Collections.Generic.List\

A list of constructor infos

|

GetDeclaredFields(Type)

Gets reflection information for all declared fields

Declaration
public static List<FieldInfo> GetDeclaredFields(this Type type)
Parameters
TypeNameDescription
System.Typetype

The class/type where the fields are declared

|

Returns
TypeDescription
System.Collections.Generic.List\

A list of fields

|

GetDeclaredMethods(Type)

Gets reflection information for all declared methods

Declaration
public static List<MethodInfo> GetDeclaredMethods(this Type type)
Parameters
TypeNameDescription
System.Typetype

The class/type where the methods are declared

|

Returns
TypeDescription
System.Collections.Generic.List\

A list of methods

|

GetDeclaredProperties(Type)

Gets reflection information for all declared properties

Declaration
public static List<PropertyInfo> GetDeclaredProperties(this Type type)
Parameters
TypeNameDescription
System.Typetype

The class/type where the properties are declared

|

Returns
TypeDescription
System.Collections.Generic.List\

A list of properties

|

GetDefaultValue(Type)

Gets default value for a specific type

Declaration
public static object GetDefaultValue(this Type type)
Parameters
TypeNameDescription
System.Typetype

The class/type

|

Returns
TypeDescription
System.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
TypeNameDescription
System.Typetype

The declaring class/type

|

Returns
TypeDescription
System.Collections.Generic.List\

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
TypeNameDescription
System.Typetype

The declaring class/type

|

Returns
TypeDescription
System.Collections.Generic.List\

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
TypeNameDescription
System.Typetype

The declaring class/type

|

Returns
TypeDescription
System.Collections.Generic.List\

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
TypeNameDescription
System.Typetype

The class/type

| | System.Type[] | parameters |

Optional parameters to target a specific overload of multiple indexers

|

Returns
TypeDescription
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(this Type type, Type[] parameters = null)
Parameters
TypeNameDescription
System.Typetype

The class/type

| | System.Type[] | parameters |

Optional parameters to target a specific overload of multiple indexers

|

Returns
TypeDescription
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(this Type type, Type[] parameters = null)
Parameters
TypeNameDescription
System.Typetype

The class/type

| | System.Type[] | parameters |

Optional parameters to target a specific overload of multiple indexers

|

Returns
TypeDescription
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(this Type type, string name)
Parameters
TypeNameDescription
System.Typetype

The class/type to start searching at

| | System.String | name |

The name of the inner type (case sensitive)

|

Returns
TypeDescription
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)

Declaration
public static IEnumerable<Type> InnerTypes(this Type type)
Parameters
TypeNameDescription
System.Typetype

The class/type to start with

|

Returns
TypeDescription
System.Collections.Generic.IEnumerable\

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
TypeNameDescription
System.Typetype

The type

|

Returns
TypeDescription
System.Boolean

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
TypeNameDescription
System.Typetype

The type

|

Returns
TypeDescription
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(this Type type)
Parameters
TypeNameDescription
System.Typetype

The type

|

Returns
TypeDescription
System.Boolean

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
TypeNameDescription
System.Typetype

The type

|

Returns
TypeDescription
System.Boolean

True if the type represents some number

|

IsStatic(Type)

Tests whether a type is static, as defined in C#

Declaration
public static bool IsStatic(this Type type)
Parameters
TypeNameDescription
System.Typetype

The type

|

Returns
TypeDescription
System.Boolean

True if the type is static

|

IsStruct(Type)

Tests if a type is a struct

Declaration
public static bool IsStruct(this Type type)
Parameters
TypeNameDescription
System.Typetype

The type

|

Returns
TypeDescription
System.Boolean

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
TypeNameDescription
System.Typetype

The type

|

Returns
TypeDescription
System.Boolean

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
TypeNameDescription
System.Typetype

The type

|

Returns
TypeDescription
System.Boolean

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
TypeNameDescription
System.Typetype

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
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type

| | System.String | name |

The name

|

Returns
TypeDescription
System.Reflection.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
TypeNameDescription
System.Typetype

The class/type

| | System.String | name |

The name

|

Returns
TypeDescription
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(this Type type, string name)
Parameters
TypeNameDescription
System.Typetype

The class/type

| | System.String | name |

The name

|

Returns
TypeDescription
System.Reflection.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 F StaticFieldRefAccess<F>(this Type type, string fieldName)
Parameters
TypeNameDescription
System.Typetype

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

| | System.String | fieldName |

The name of the field

|

Returns
TypeDescription
F

A readable/assignable reference to the field

|

Type Parameters
NameDescription
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(this Type type, params string[] names)
Parameters
TypeNameDescription
System.Typetype

The type that is involved

| | System.String[] | names |

A list of names

|