Introspection
Introspection (also called reflection) allows you to discover the type of an object and its members at runtime and manipulate them dynamically. This is useful for various scenarios, such as serialization, object mapping, dependency injection, and dynamic proxies. AddyScript provides a comprehensive introspection API that enables you to inspect types, fields, properties, methods, and events.
Type Information
You can obtain type information from a class using the typeof operator, which returns a TypeInfo object representing the specified type.
You can also get the type of an instance using the type property of the object.
The TypeInfo class provides various properties and methods to inspect the type.
They are listed in the table below:
| Member | Nature | Description |
|---|---|---|
string name { read; } |
Property | Gets the name of the type. |
TypeInfo superType { read; } |
Property | Gets the base type of the type. |
bool isIntegral { read; } |
Property | Indicates whether the type is an integral type. |
bool isNumeric { read; } |
Property | Indicates whether the type is a numeric type. |
bool isTemporal { read; } |
Property | Indicates whether the type is a temporal type. |
bool isSequential { read; } |
Property | Indicates whether the type is a sequential type. |
bool isCollection { read; } |
Property | Indicates whether the type is a collection type. |
bool isSubclassOf(TypeInfo otherType) |
Method | Determines whether the current type is a subclass of the specified type. |
bool isAssignableTo(TypeInfo otherType) |
Method | Determines whether instances of the current type can be assigned to variables of the specified type. |
bool isAssignableFrom(TypeInfo otherType) |
Method | Determines whether instances of the specified type can be assigned to variables of the current type. |
MethodInfo $constructor { read; } |
Property | Gets the constructor method of the type. |
PropertyInfo indexer { read; } |
Property | Gets the indexer property of the type, if any. |
map fields { read; } |
Property | Gets a map of fields defined in the type identified by their names. |
map properties { read; } |
Property | Gets a map of properties defined in the type identified by their names. |
map methods { read; } |
Property | Gets a map of methods defined in the type identified by their names. |
map events { read; } |
Property | Gets a map of events defined in the type identified by their names. |
list attributes { read; } |
Property | Gets a list of attributes applied to the type. |
any newInstance(..args) |
Method | Creates a new instance of the type by invoking its constructor. |
Member Information
The members of a type can be inspected using various classes. All are based on a common MemberInfo class that provides basic information about a member, such as its name, scope, and modifier.
MemberInfo
The MemberInfo class provides the following members:
| Member | Nature | Description |
|---|---|---|
string name { read; } |
Property | Gets the name of the member. |
string fullName { read; } |
Property | Gets the fully qualified name of the member. |
TypeInfo holder { read; } |
Property | Gets the type that declares the member. |
string scope { read; } |
Property | Gets the scope of the member (Private, Protected, Public). |
string modifier { read; } |
Property | Gets the modifier of the member (Static, Final, Abstract, etc.). |
list attributes { read; } |
Property | Gets a list of attributes applied to the member. |
The following subclasses of MemberInfo provide additional information specific to the type of member.
FieldInfo
The FieldInfo class represents a field in a type and provides the following additional members:
| Member | Nature | Description |
|---|---|---|
any sharedValue { read; } |
Property | Gets the value of the static field. |
any getValue(any target) |
Method | Gets the value of the field for the specified target object. |
void setValue(any target, any value) |
Method | Sets the value of the field for the specified target object. |
PropertyInfo
The PropertyInfo class represents a property in a type and provides the following additional members:
| Member | Nature | Description |
|---|---|---|
bool canRead { read; } |
Property | Indicates whether the property has a reader. |
bool canWrite { read; } |
Property | Indicates whether the property has a writer. |
MethodInfo reader { read; } |
Property | Gets the reader method of the property. |
MethodInfo writer { read; } |
Property | Gets the writer method of the property. |
any getValue(any target) |
Method | Gets the value of the property for the specified target object. |
void setValue(any target, any value) |
Method | Sets the value of the property for the specified target object. |
any getItem(any target, any index) |
Method | Gets the value of the property at the given index for the specified target object. |
void setItem(any target, any index, any value) |
Method | Sets the value of the property at the given index for the specified target object. |
MethodInfo
The MethodInfo class represents a method in a type and provides the following additional members:
| Member | Nature | Description |
|---|---|---|
list parameters { read; } |
Property | Gets a list of parameters defined for the method. |
any invoke(any target, ..args) |
Method | Invokes the method on the specified target object with the given arguments. |
EventInfo
The EventInfo class represents an event in a type and provides the following additional members:
| Member | Nature | Description |
|---|---|---|
list parameters { read; } |
Property | Gets a list of parameters defined for the event. |
ParameterInfo
The ParameterInfo class represents a parameter of a method or event and provides the following members:
| Member | Nature | Description |
|---|---|---|
string name { read; } |
Property | Gets the name of the parameter. |
bool byRef { read; } |
Property | Indicates whether the parameter is passed by reference. |
bool vaList { read; } |
Property | Indicates whether the parameter is a variadic parameter. |
bool canBeEmpty { read; } |
Property | Indicates whether the parameter can be a null reference or an empty string or collection. |
any defaultValue { read; } |
Property | Gets the default value of the parameter, if any. |
Introspection showcase
The following example shows how this functionality is handled in AddyScript.
| Introspection demo | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
The above code will output detailed information about the Exception, int, tuple, and map types, including their fields, properties, methods, and events. It will also demonstrate how to create instances and invoke methods using reflection.