The runtime metadata for a script type.
Julian script types, be it built-in or user-defined, class-based or primitive, all have an engine-scoped singleton Type object that carries the type's metadata throughout the engine lifecycle. A Type object is usually the starting point of any reflection-based logic.
There are three ways to obtain Type in runtime. If type is known at the time the code is written, one can use typeof
operator on a type name: Type t0 = typeof(MyModule.MyClass);
Type t1 = typeof(string);
Type t2 = typeof(int[]);
Alternatively, we can also retrieve the type of a runtime object via getType() method: MyClass inst = new MyClass();
... ...
Type t0 = inst.getType(inst);
Type t1 = typeof(MyClass);
bool same = t0 == t1; // true
The third and the most flexible way is to get type dynamically by Type.load(). If the type is not loaded, load()
will trigger type loading, same to what happens when a type is used for the first time. Otherwise the Type object will be immediately returned from engine's type storage.
Type usually serves as a starting point for invoking reflection API. For more details, see here.
Parent Class
Type | Name | Signature |
---|---|---|
method | getAttributes | public Attribute[] getAttributes() |
method | getConstructors | public Constructor[] getConstructors() |
method | getExtensions | public Type[] getExtensions() |
method | getField | public Field getField(string) |
method | getFields | public Field[] getFields() |
method | getFullName | public String getFullName() |
method | getInterfaces | public Type[] getInterfaces() |
method | getMethods | public Method[] getMethods() |
method | getMethods | public Method[] getMethods(string) |
method | getModule | public Module getModule() |
method | getModuleName | public String getModuleName() |
method | getParent | public Type getParent() |
method | getSimpleName | public String getSimpleName() |
method | isArray | public bool isArray() |
method | isClass | public bool isClass() |
method | isFinal | public bool isFinal() |
method | isInterface | public bool isInterface() |
method | isPrimitive | public bool isPrimitive() |
method S | load | public static Type load(string) |
method | toString | public String toString() |
public Attribute[] getAttributes()
Get all the attributes annotated on the type definition.
The result array contains each instance of Attribute placed at the type definition. For example: [Owner(name="Joshua", opened=2015)]
[Protection(enabled=false)]
class Account { }
Attribute[] attrs = typeof(Account).getAttributes(); // length = 2: Owner and Protection
Returns
public Constructor[] getConstructors()
Get the constructors of this type, in no particular order.
For system types, this will only return constructors that are marked as visible to reflection. For user-defined types, this will return all constructors, regardless of its defined visibility, unless it's marked as invisible. See Reflected for its detailed usage.
Returns
public Type[] getExtensions()
Get the extension types that are directly installed on this type.
The result array will only include those that are declared as the extension types for the current type. In particular, it doesn't include any extension types that can be recursively discovered by tracing through the type definition chain. In one example: static class ExtA { }
interface A : ExtA { } // Install ExtA to A
static class ExtB { }
interface B : A, ExtB { } // Extend interface A from B and install ExtB to it
Type[] types1 = typeof(B).getExtensions(); // size = 1: ExtA
Returns
public Field getField(string name)
Get the field of this type with specified name.
This method applies a simple filter on top of getFields()
. For more information on the mechanic of method retrieval, refer to the documentation of that method.
Parameters
Returns
public Field[] getFields()
Get the fields of this type, in no particular order.
For system types, this will only return fields that are marked as visible to reflection. For user-defined types, this will return all fields that are visible inside this type, unless it's marked as invisible. See Reflected for the usage of attribute.
Fields that are visible inside this type include all field members defined on this type, as well as all the field members inherited from ancestors.
Returns
public String getFullName()
Get the fully qualified type name. This will always return canonical name, not language-level alias for certain built-in types.
Returns
public Type[] getInterfaces()
Get the directly implemented or extended interfaces.
The result array will only include those that are declared as the interface types for the current type. In particular, it doesn't include any interfaces that can be recursively discovered by tracing through the type definition chain. In one example: interface A { }
interface B : A { }
interface C { }
class MyClass : B, C { }
Type[] types0 = typeof(MyClass).getInterfaces(); // size = 2: B and C
Type[] types1 = typeof(B).getInterfaces(); // size = 1: A
Returns
public Method[] getMethods()
Get the methods of this type, in no particular order.
For system types, this will only return methods that are marked as visible to reflection. For user-defined types, this will return all methods that are visible inside this type, unless it's marked as invisible. See Reflected for the usage of attribute.
Methods that are visible inside this type include all method members defined on this type, as well as all the method members inherited from ancestors yet not hidden by a new member with same signature. So if a protected member is not overridden, it will be showing up in the result array. If it's been overridden in this type, the latest definition will be included in the array instead: class Parent {
void fun1(){ }
void fun2(){ }
}
class Child : Parent {
void fun1(){ } // Overrides fun1
}
// Returns an array of size = 2, including Parent.fun2() and Child.fun1()
Method[] methods = typeof(Child).getMethods();
Returns
public Method[] getMethods(string name)
Get the methods of this type with specified name, in no particular order.
This method applies a simple filter on top of getMethods()
. For more information on the mechanic of method retrieval, refer to the documentation of that method. For more refined lookup, recommend using IIterable's extension API.
Parameters
Returns
public Module getModule()
Get the module this type belongs to. Note primitive and some built-in types such as Object do not belong to any module.
Returns
public String getModuleName()
Get the module name of this type, i.e. excluding the part for module. For a type with fully qualified name "A.B.C", this method will return "A.B".
Returns
public Type getParent()
Get the parent type of this type. A class type, other than Object, must have one parent. Calling this method on Object, interface, or other non-class types always returns null.
Returns
public String getSimpleName()
Get the simple name of this type, i.e. excluding the part for module. For a type with fully qualified name "A.B.C", this method will return "C". This will always return canonical name, not language-level alias for certain built-in types.
Returns
public bool isArray()
Check whether the type is an Array type. Array type is also a class type.
Returns
public bool isClass()
Check whether the type is a class type. Interface types, primitive types, Any and Void type is not class type; all the others are.
Returns
public bool isFinal()
Check whether the type is finalized and thus cannot be inherited.
Returns
public bool isInterface()
Check whether the type is an interface type.
Returns
public bool isPrimitive()
Check whether the type is primitive. A type is primitive if it's int, byte, float, bool or char. Any other types, including string, any and array type with primitive elements, are not primitive.
Returns
public static Type load(string typeName)
Load a type with specified name. If the type is not loaded, this method will trigger loading of the entire dependency closure of the required type. Otherwise the Type object will be immediately returned from engine's type storage. This method is thread-safe.
Some examples: Type.load("int");
Type.load("Integer");
Type.load("byte[]");
Type.load("MyModule.MyClass");
Type.load("UrClass[][]");
Required Policies
System.Reflection/load
Parameters
int
instead of Integer).Returns
Throws
getCause()
to inspect the original error.public String toString()
Get a string representation of this type.
Returns
[TYPE|full-name]
. Example: [TYPE|MyModule.MyClass]