ReflectionUtility¶
Namespace:
SideXP.Core.Reflection
Miscelaneous functions for working with C# reflection.
Fields¶
InstanceFlags¶
Targets elements declared on the instance, public and non-public.
StaticFlags¶
Targets elements declared static, public and non-public.
NonProjectAssemblies¶
The name of the assemblies that are included in Unity C# subset, but are not part of the project.
Methods¶
GetAllAssemblies(IList<string>)¶
Gets all assemblies in the current app domain.
Parameters
excludedAssembliesPrefixes: The prefixes of the assembly names to exclude from this query.
Returns
Returns the found assemblies.
GetProjectAssemblies()¶
Get all assemblies related to the current Unity project.
Returns
Returns the found assemblies.
FindType(string, string, Type)¶
Gets a type in the project by its full name (namespace and type name).
Parameters
className: The name of the class to find.namespaceName: The name of the namespace that should contain the type.type: Outputs the found type.
Returns
Returns true if a type has been found.
TryGetAttribute(MemberInfo, Type, Attribute, bool)¶
public static bool TryGetAttribute(MemberInfo member, Type attributeType, out Attribute attribute, bool inherit = true)
Tries to get an attribute of a given type defined on a given member.
Parameters
member: The member from which to get the attribute.attributeType: The type of the attribute to get. Assumes it inherits fromAttribute, and the member only uses it once.attribute: Outputs the found attribute.inherit: If enabled, the ancestors of the given member are also checked.
Returns
Returns true if the custom attribute has been found.
TryGetAttribute<TAttribute>(MemberInfo, TAttribute, bool)¶
public static bool TryGetAttribute<TAttribute>(MemberInfo member, out TAttribute attribute, bool inherit = true)
Tries to get an attribute of a given type defined on a given member.
Type parameters
TAttribute
Parameters
member: The member from which to get the attribute.attribute: Outputs the found attribute.inherit: If enabled, the ancestors of the given member are also checked.
Returns
Returns true if the custom attribute has been found.
TryGetAttributes(MemberInfo, Type, Attribute[], bool)¶
public static bool TryGetAttributes(MemberInfo member, Type attributeType, out Attribute[] attributes, bool inherit = true)
Tries to get the attributes of a given type defined on a given member.
Parameters
member: The member from which to get the attributes.attributeType: The type of the attributes to get.attributes: Outputs the found attributes.inherit: If enabled, the ancestors of the given member are also checked.
Returns
Returns true if at least one attribute of the given type has been found.
TryGetAttributes<TAttribute>(MemberInfo, TAttribute[], bool)¶
public static bool TryGetAttributes<TAttribute>(MemberInfo member, out TAttribute[] attributes, bool inherit = true)
Tries to get the attributes of a given type defined on a given member.
Type parameters
TAttribute
Parameters
member: The member from which to get the attributes.attributes: Outputs the found attributes.inherit: If enabled, the ancestors of the given member are also checked.
Returns
Returns true if at least one attribute of the given type has been found.
HasAttribute(MemberInfo, Type, bool)¶
Checks if a given member has a given attribute.
Parameters
member: The member from which to check the attribute.attributeType: The type of the attribute to check.inherit: If enabled, the ancestors of the given member are also checked.
Returns
Returns true if the given memebr has an attribute of the given type.
HasAttribute<TAttribute>(MemberInfo, bool)¶
Checks if a given member has a given attribute.
Type parameters
TAttribute
Parameters
member: The member from which to check the attribute.inherit: If enabled, the ancestors of the given member are also checked.
Returns
Returns true if the given memebr has an attribute of the given type.
IsExposed(MemberInfo)¶
Checks if a given member (field or property) is exposed (public or private serialized).
Parameters
member: The member to check.
Returns
Returns true if the given member is exposed.
IsPublic(MemberInfo)¶
Checks if a given member (field or property) is public.
Parameters
member: The member to check.
Returns
Returns true if the given member is public.
Remarks
A property is considered public if one of its accessors is marked as public.
IsPrivate(MemberInfo)¶
Checks if a given member (field or property) is private.
Parameters
member: The member to check.
Returns
Returns true if the given member is private.
Remarks
A property is considered private if its accessors are both marked as private.
GetExposedFields(Type, bool)¶
Gets all the public or private serialized fields of a given type.
Parameters
type: The type of which to get the exposed fields.inherit: If enabled, also get the fields from the given type's ancestors.
Returns
Returns the found public or private serialized fields of the given type.
GetFieldOrProperty(Type, string, bool, BindingFlags)¶
public static FieldOrPropertyInfo GetFieldOrProperty(Type type, string name, bool inherited = false, BindingFlags bindingFlags = Instance | Public | NonPublic)
Gets informations about a named field or property from a given type.
Parameters
type: The type from which to get the member info.name: The name of the member to get.inherited: If enabled, this function will try to get the named field or property from the given type, and from any of its base types.bindingFlags: The binding flags used for querying the member.
Returns
Returns the informations about the found field or property.
GetFieldOrProperty(Type, string, FieldOrPropertyInfo, bool, BindingFlags)¶
public static bool GetFieldOrProperty(Type type, string name, out FieldOrPropertyInfo info, bool inherited = false, BindingFlags bindingFlags = Instance | Public | NonPublic)
Tries to get informations about a named field or property from a given type.
Parameters
info: Outputs the informations about the found field or property.type: The type from which to get the member info.name: The name of the member to get.inherited: If enabled, this function will try to get the named field or property from the given type, and from any of its base types.bindingFlags: The binding flags used for querying the member.
Returns
Returns true if the named field or property has been found.
GetFieldOrProperty(object, string, bool, BindingFlags)¶
public static FieldOrPropertyInfo GetFieldOrProperty(object target, string name, bool inherited = false, BindingFlags bindingFlags = Instance | Public | NonPublic)
Gets informations about a named field or property from a given type.
Parameters
target: The object from which to get the member info.name: The name of the member to get.inherited: If enabled, this function will try to get the named field or property from the given type, and from any of its base types.bindingFlags: The binding flags used for querying the member.
Returns
Returns the informations about the found field or property.
GetFieldOrProperty(object, string, FieldOrPropertyInfo, bool, BindingFlags)¶
public static bool GetFieldOrProperty(object target, string name, out FieldOrPropertyInfo info, bool inherited = false, BindingFlags bindingFlags = Instance | Public | NonPublic)
Parameters
target: The object from which to get the member info.
GetFieldOrProperty<T>(string, bool, BindingFlags)¶
public static FieldOrPropertyInfo GetFieldOrProperty<T>(string name, bool inherited = false, BindingFlags bindingFlags = Instance | Public | NonPublic)
Gets informations about a named field or property from a given type.
Type parameters
T
Parameters
name: The name of the member to get.inherited: If enabled, this function will try to get the named field or property from the given type, and from any of its base types.bindingFlags: The binding flags used for querying the member.
Returns
Returns the informations about the found field or property.
GetFieldOrProperty<T>(string, FieldOrPropertyInfo, bool, BindingFlags)¶
public static bool GetFieldOrProperty<T>(string name, out FieldOrPropertyInfo info, bool inherited = false, BindingFlags bindingFlags = Instance | Public | NonPublic)
Type parameters
T
GetFieldsAndProperties(Type, bool, BindingFlags)¶
public static FieldOrPropertyInfo[] GetFieldsAndProperties(Type type, bool inherited = false, BindingFlags bindingFlags = Instance | Public | NonPublic)
Gets informations about all the fields and properties from a given type.
Parameters
type: The type from which to get the informations.inherited: If enabled, this function will query all the fields and properties from the given type and all its base types.bindingFlags: The binding flags used for querying the member.
Returns
Returns the informations ahout the found fields and properties;
GetFieldsAndProperties(object, bool, BindingFlags)¶
public static FieldOrPropertyInfo[] GetFieldsAndProperties(object target, bool inherited = false, BindingFlags bindingFlags = Instance | Public | NonPublic)
Gets informations about all the fields and properties from a given type.
Parameters
target: The object from which to get the informations.inherited: If enabled, this function will query all the fields and properties from the given type and all its base types.bindingFlags: The binding flags used for querying the member.
Returns
Returns the informations ahout the found fields and properties;
GetFieldsAndProperties<T>(bool, BindingFlags)¶
public static FieldOrPropertyInfo[] GetFieldsAndProperties<T>(bool inherited = false, BindingFlags bindingFlags = Instance | Public | NonPublic)
Gets informations about all the fields and properties from a given type.
Type parameters
T
Parameters
inherited: If enabled, this function will query all the fields and properties from the given type and all its base types.bindingFlags: The binding flags used for querying the member.
Returns
Returns the informations ahout the found fields and properties;
GetFieldOrPropertyFromPath(Type, string, bool, BindingFlags)¶
public static FieldOrPropertyInfo GetFieldOrPropertyFromPath(Type type, string path, bool inherited = false, BindingFlags bindingFlags = Instance | Public | NonPublic)
Gets the field or property info from property path.
Parameters
type: The type from which you want to parse the path.path: The property path (as you can get it frompropertyPath).inherited: If enabled, this function will try to get the named field or property from the given type, and from any of its base types.bindingFlags: The binding flags used for querying the member.
Returns
Returns the found field or property informations.
GetFieldOrPropertyFromPath(Type, string, FieldOrPropertyInfo, bool, BindingFlags)¶
public static bool GetFieldOrPropertyFromPath(Type type, string path, out FieldOrPropertyInfo info, bool inherited = false, BindingFlags bindingFlags = Instance | Public | NonPublic)
Gets the field or property info from property path.
Parameters
info: Outputs the found field or property informations.type: The type from which you want to parse the path.path: The property path (as you can get it frompropertyPath).
Returns
Returns the found field or property informations.
GetNestedObject(object, string)¶
Gets a reference or value of an object given a property path (like myObject.myContainer[3].myProperty). This function is inspired by the SpacePuppy Unity Framework: https://github.com/lordofduct/spacepuppy-unity-framework-4.0/blob/master/Framework/com.spacepuppy.core/Editor/src/EditorHelper.cs
Parameters
source: The object in which you want to get the nested object.propertyPath: The property path for navigating to the expected object (like myObject.myContainer[3].myProperty).
Returns
Returns the found nested object, or null if the path didn't lead to a valid object.
GetNestedObject<T>(object, string)¶
Gets a reference or value of an object given a property path (like myObject.myContainer[3].myProperty). This function is inspired by the SpacePuppy Unity Framework: https://github.com/lordofduct/spacepuppy-unity-framework-4.0/blob/master/Framework/com.spacepuppy.core/Editor/src/EditorHelper.cs
Type parameters
T: The type of the expected target object.
Parameters
source: The object in which you want to get the nested object.propertyPath: The property path for navigating to the expected object (like myObject.myContainer[3].myProperty).
Returns
Returns the found nested object, or null if the path didn't lead to a valid object.