Class AccessToolsExtensions +
+Adds extensions to Type for a lot of AccessTools methods
+Inheritance
+Inherited Members
+Namespace: HarmonyLib
+Assembly: 0Harmony.dll
+Syntax
+public static class AccessToolsExtensions
+ Methods +
+ + | + Improve this Doc + + + View Source + + +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
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type where the constructor is declared + |
+
System.Type[] | +parameters | +Optional parameters to target a specific overload of the method + |
+
System.Boolean | +searchForStatic | +Optional parameters to only consider static constructors + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.ConstructorInfo | +A constructor info or null when type is null or when the method cannot be found + |
+
CreateInstance(Type)
+Creates an (possibly uninitialized) instance of a given type
+Declaration
+public static object CreateInstance(this Type type)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type + |
+
Returns
+Type | +Description | +
---|---|
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
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type where the constructor is declared + |
+
System.Type[] | +parameters | +Optional parameters to target a specific overload of the constructor + |
+
System.Boolean | +searchForStatic | +Optional parameters to only consider static constructors + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.ConstructorInfo | +A constructor info or null when type is null or when the constructor cannot be found + |
+
DeclaredField(Type, Int32)
+Gets the reflection information for a field
+Declaration
+public static FieldInfo DeclaredField(this Type type, int idx)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type where the field is declared + |
+
System.Int32 | +idx | +The zero-based index of the field inside the class definition + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.FieldInfo | +A field or null when type is null or when the field cannot be found + |
+
DeclaredField(Type, String)
+Gets the reflection information for a directly declared field
+Declaration
+public static FieldInfo DeclaredField(this Type type, string name)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type where the field is defined + |
+
System.String | +name | +The name of the field + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.FieldInfo | +A field or null when type/name is null or when the field cannot be found + |
+
DeclaredIndexer(Type, Type[])
+Gets the reflection information for a directly declared indexer property
+Declaration
+public static PropertyInfo DeclaredIndexer(this Type type, Type[] parameters = null)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type where the indexer property is declared + |
+
System.Type[] | +parameters | +Optional parameters to target a specific overload of multiple indexers + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.PropertyInfo | +An indexer property or null when type is null or when it cannot be found + |
+
DeclaredIndexerGetter(Type, Type[])
+Gets the reflection information for the getter method of a directly declared indexer property
+Declaration
+public static MethodInfo DeclaredIndexerGetter(this Type type, Type[] parameters = null)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type where the indexer property is declared + |
+
System.Type[] | +parameters | +Optional parameters to target a specific overload of multiple indexers + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.MethodInfo | +A method or null when type is null or when indexer property cannot be found + |
+
DeclaredIndexerSetter(Type, Type[])
+Gets the reflection information for the setter method of a directly declared indexer property
+Declaration
+public static MethodInfo DeclaredIndexerSetter(this Type type, Type[] parameters)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type where the indexer property is declared + |
+
System.Type[] | +parameters | +Optional parameters to target a specific overload of multiple indexers + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.MethodInfo | +A method or null when type is null or when indexer property cannot be found + |
+
DeclaredMethod(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 | +
---|---|---|
System.Type | +type | +The class/type where the method is declared + |
+
System.String | +name | +The name of the method (case sensitive) + |
+
System.Type[] | +parameters | +Optional parameters to target a specific overload of the method + |
+
System.Type[] | +generics | +Optional list of types that define the generic version of the method + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.MethodInfo | +A method or null when type/name is null or when the method cannot be found + |
+
DeclaredProperty(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 | +
---|---|---|
System.Type | +type | +The class/type where the property is declared + |
+
System.String | +name | +The name of the property (case sensitive) + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.PropertyInfo | +A property or null when type/name is null or when the property cannot be found + |
+
DeclaredPropertyGetter(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 | +
---|---|---|
System.Type | +type | +The class/type where the property is declared + |
+
System.String | +name | +The name of the property (case sensitive) + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.MethodInfo | +A method or null when type/name is null or when the property cannot be found + |
+
DeclaredPropertySetter(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 | +
---|---|---|
System.Type | +type | +The class/type where the property is declared + |
+
System.String | +name | +The name of the property (case sensitive) + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.MethodInfo | +A method or null when type/name is null or when the property cannot be found + |
+
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 | +
---|---|---|
System.Type | +type | +The class/type where the field is defined + |
+
System.String | +name | +The name of the field (case sensitive) + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.FieldInfo | +A field or null when type/name is null or when the field cannot be found + |
+
FieldRefAccess<F>(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 | +
---|---|---|
System.Type | +type | +The type that defines the field, or derived class of this type; must not be a struct type unless the field is static + |
+
System.String | +fieldName | +The name of the field + |
+
Returns
+Type | +Description | +
---|---|
AccessTools.FieldRef<System.Object, F> | +A readable/assignable |
+
Type Parameters
+Name | +Description | +
---|---|
F | +The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), +a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, +either that type or the underlying integral type of that enum type + |
+
Remarks
+
+This method is meant for cases where the given type is only known at runtime and thus can't be used as a type parameter T
+in e.g.
+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. +
+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 | +
---|---|---|
System.Type | +type | +The class/type to start with + |
+
System.Func<System.Type, T> | +func | +The evaluation function returning T + |
+
Returns
+Type | +Description | +
---|---|
T | +The first non- |
+
Type Parameters
+Name | +Description | +
---|---|
T | +Result type of func() + |
+
Remarks
+The type hierarchy of a class or value type (including struct) does NOT include implemented interfaces, +and the type hierarchy of an interface is only itself (regardless of whether that interface implements other interfaces). +The top-most type in the type hierarchy of all non-interface types (including value types) is System.Object.
+FindIncludingInnerTypes<T>(Type, Func<Type, T>)
+Applies a function going into inner types and stops at the first non-null
result
Declaration
+public static T FindIncludingInnerTypes<T>(this Type type, Func<Type, T> func)
+ where T : class
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type to start with + |
+
System.Func<System.Type, T> | +func | +The evaluation function returning T + |
+
Returns
+Type | +Description | +
---|---|
T | +The first non- |
+
Type Parameters
+Name | +Description | +
---|---|
T | +Generic type parameter + |
+
FirstConstructor(Type, Func<ConstructorInfo, Boolean>)
+Given a type, returns the first constructor matching a predicate
+Declaration
+public static ConstructorInfo FirstConstructor(this Type type, Func<ConstructorInfo, bool> predicate)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type to start searching at + |
+
System.Func<System.Reflection.ConstructorInfo, System.Boolean> | +predicate | +The predicate to search with + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.ConstructorInfo | +The constructor info or null if type/predicate is null or if a type with that name cannot be found + |
+
FirstInner(Type, Func<Type, Boolean>)
+Given a type, returns the first inner type matching a recursive search with a predicate
+Declaration
+public static Type FirstInner(this Type type, Func<Type, bool> predicate)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type to start searching at + |
+
System.Func<System.Type, System.Boolean> | +predicate | +The predicate to search with + |
+
Returns
+Type | +Description | +
---|---|
System.Type | +The inner type or null if type/predicate is null or if a type with that name cannot be found + |
+
FirstMethod(Type, Func<MethodInfo, Boolean>)
+Given a type, returns the first method matching a predicate
+Declaration
+public static MethodInfo FirstMethod(this Type type, Func<MethodInfo, bool> predicate)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type to start searching at + |
+
System.Func<System.Reflection.MethodInfo, System.Boolean> | +predicate | +The predicate to search with + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.MethodInfo | +The method or null if type/predicate is null or if a type with that name cannot be found + |
+
FirstProperty(Type, Func<PropertyInfo, Boolean>)
+Given a type, returns the first property matching a predicate
+Declaration
+public static PropertyInfo FirstProperty(this Type type, Func<PropertyInfo, bool> predicate)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type to start searching at + |
+
System.Func<System.Reflection.PropertyInfo, System.Boolean> | +predicate | +The predicate to search with + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.PropertyInfo | +The property or null if type/predicate is null or if a type with that name cannot be found + |
+
GetDeclaredConstructors(Type, Nullable<Boolean>)
+Gets reflection information for all declared constructors
+Declaration
+public static List<ConstructorInfo> GetDeclaredConstructors(this Type type, bool? searchForStatic = null)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type where the constructors are declared + |
+
System.Nullable<System.Boolean> | +searchForStatic | +Optional parameters to only consider static constructors + |
+
Returns
+Type | +Description | +
---|---|
System.Collections.Generic.List<System.Reflection.ConstructorInfo> | +A list of constructor infos + |
+
GetDeclaredFields(Type)
+Gets reflection information for all declared fields
+Declaration
+public static List<FieldInfo> GetDeclaredFields(this Type type)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type where the fields are declared + |
+
Returns
+Type | +Description | +
---|---|
System.Collections.Generic.List<System.Reflection.FieldInfo> | +A list of fields + |
+
GetDeclaredMethods(Type)
+Gets reflection information for all declared methods
+Declaration
+public static List<MethodInfo> GetDeclaredMethods(this Type type)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type where the methods are declared + |
+
Returns
+Type | +Description | +
---|---|
System.Collections.Generic.List<System.Reflection.MethodInfo> | +A list of methods + |
+
GetDeclaredProperties(Type)
+Gets reflection information for all declared properties
+Declaration
+public static List<PropertyInfo> GetDeclaredProperties(this Type type)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type where the properties are declared + |
+
Returns
+Type | +Description | +
---|---|
System.Collections.Generic.List<System.Reflection.PropertyInfo> | +A list of properties + |
+
GetDefaultValue(Type)
+Gets default value for a specific type
+Declaration
+public static object GetDefaultValue(this Type type)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type + |
+
Returns
+Type | +Description | +
---|---|
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
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The declaring class/type + |
+
Returns
+Type | +Description | +
---|---|
System.Collections.Generic.List<System.String> | +A list of field names + |
+
GetMethodNames(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 | +
---|---|---|
System.Type | +type | +The declaring class/type + |
+
Returns
+Type | +Description | +
---|---|
System.Collections.Generic.List<System.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 | +
---|---|---|
System.Type | +type | +The declaring class/type + |
+
Returns
+Type | +Description | +
---|---|
System.Collections.Generic.List<System.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 | +
---|---|---|
System.Type | +type | +The class/type + |
+
System.Type[] | +parameters | +Optional parameters to target a specific overload of multiple indexers + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.PropertyInfo | +An indexer property or null when type is null or when it cannot be found + |
+
IndexerGetter(Type, Type[])
+Gets the reflection information for the getter method of an indexer property by searching the type and all its super types
+Declaration
+public static MethodInfo IndexerGetter(this Type type, Type[] parameters = null)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type + |
+
System.Type[] | +parameters | +Optional parameters to target a specific overload of multiple indexers + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.MethodInfo | +A method or null when type is null or when the indexer property cannot be found + |
+
IndexerSetter(Type, Type[])
+Gets the reflection information for the setter method of an indexer property by searching the type and all its super types
+Declaration
+public static MethodInfo IndexerSetter(this Type type, Type[] parameters = null)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type + |
+
System.Type[] | +parameters | +Optional parameters to target a specific overload of multiple indexers + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.MethodInfo | +A method or null when type is null or when the indexer property cannot be found + |
+
Inner(Type, String)
+Given a type, returns the first inner type matching a recursive search by name
+Declaration
+public static Type Inner(this Type type, string name)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type to start searching at + |
+
System.String | +name | +The name of the inner type (case sensitive) + |
+
Returns
+Type | +Description | +
---|---|
System.Type | +The inner type or null if type/name is null or if a type with that name cannot be found + |
+
InnerTypes(Type)
+Enumerates all inner types (non-recursive)
+Declaration
+public static IEnumerable<Type> InnerTypes(this Type type)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type to start with + |
+
Returns
+Type | +Description | +
---|---|
System.Collections.Generic.IEnumerable<System.Type> | +An enumeration of all inner System.Type + |
+
IsClass(Type)
+Tests if a type is a class
+Declaration
+public static bool IsClass(this Type type)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The type + |
+
Returns
+Type | +Description | +
---|---|
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
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The type + |
+
Returns
+Type | +Description | +
---|---|
System.Boolean | +True if the type represents some floating point + |
+
IsInteger(Type)
+Tests if a type is an integer type
+Declaration
+public static bool IsInteger(this Type type)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The type + |
+
Returns
+Type | +Description | +
---|---|
System.Boolean | +True if the type represents some integer + |
+
IsNumber(Type)
+Tests if a type is a numerical type
+Declaration
+public static bool IsNumber(this Type type)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The type + |
+
Returns
+Type | +Description | +
---|---|
System.Boolean | +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 | +
---|---|---|
System.Type | +type | +The type + |
+
Returns
+Type | +Description | +
---|---|
System.Boolean | +True if the type is a struct + |
+
IsValue(Type)
+Tests if a type is a value type
+Declaration
+public static bool IsValue(this Type type)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The type + |
+
Returns
+Type | +Description | +
---|---|
System.Boolean | +True if the type is a value type + |
+
IsVoid(Type)
+Tests if a type is void
+Declaration
+public static bool IsVoid(this Type type)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The type + |
+
Returns
+Type | +Description | +
---|---|
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
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The class/type where the method is declared + |
+
System.String | +name | +The name of the method (case sensitive) + |
+
System.Type[] | +parameters | +Optional parameters to target a specific overload of the method + |
+
System.Type[] | +generics | +Optional list of types that define the generic version of the method + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.MethodInfo | +A method or null when type/name is null or when the method cannot be found + |
+
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 | +
---|---|---|
System.Type | +type | +The class/type + |
+
System.String | +name | +The name + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.PropertyInfo | +A property or null when type/name is null or when the property cannot be found + |
+
PropertyGetter(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 | +
---|---|---|
System.Type | +type | +The class/type + |
+
System.String | +name | +The name + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.MethodInfo | +A method or null when type/name is null or when the property cannot be found + |
+
PropertySetter(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 | +
---|---|---|
System.Type | +type | +The class/type + |
+
System.String | +name | +The name + |
+
Returns
+Type | +Description | +
---|---|
System.Reflection.MethodInfo | +A method or null when type/name is null or when the property cannot be found + |
+
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 | +
---|---|---|
System.Type | +type | +The type (can be class or struct) the field is defined in + |
+
System.String | +fieldName | +The name of the field + |
+
Returns
+Type | +Description | +
---|---|
F | +A readable/assignable reference to the field + |
+
Type Parameters
+Name | +Description | +
---|---|
F | +The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), +a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, +either that type or the underlying integral type of that enum type + |
+
ThrowMissingMemberException(Type, String[])
+Throws a missing member runtime exception
+Declaration
+public static void ThrowMissingMemberException(this Type type, params string[] names)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
System.Type | +type | +The type that is involved + |
+
System.String[] | +names | +A list of names + |
+