typeOf()

The typeOf system function returns a platform-specific reference to the specified type at runtime. This can be useful for using Reflection, passing type references to a method or attribute, or otherwise working dynamically with type metadata.

typeOf() accepts as its single parameter either an arbitrary expression, or the name of a specific type know at compile time. If an arbitrary expression is passed, the concrete type will be determined at runtime.

The return type of typeOf depends on the platform:

Elements RTL provides a platform-agnostic wrapper for these types that can be used to do reflection tasks in a cross-platform manner: RemObjects.Elements.RTL.Reflection.Type.

Examples (using .NET's System.Type):

var stringTypeRef := typeOf(String);
var lengthProperty := stringTypeRef.GetProperty("Length");

var someString := "abc";
var len := lengthProperty.GetValue(someString, nil); // 3
var stringTypeRef = typeOf(String);
var lengthProperty = stringTypeRef.GetProperty("Length");

var someString = "abc";
var len = lengthProperty.GetValue(someString, nil); // 3
let stringTypeRef = typeOf(String)
let lengthProperty = stringTypeRef.GetProperty("Length")

let someString = "abc"
let len = lengthProperty.GetValue(someString, nil); // 3
var stringTypeRef = typeof(String);
var lengthProperty = stringTypeRef.GetProperty("Length");

var someString = "abc";
var len = lengthProperty.GetValue(someString, nil); // 3

In C#, the typeof keyword works identically to the typeOf function. In Swift, the dynamicType() keyword performs the same function.

typeOf() and Generics

If you use typeOf() to obtain an open generic type (i.e. one without concrete type arguments), you can specify the number of generic parameters.

var myListType := typeOf(List<1>); //<-- 1 argument
var myDictionaryType := typeOf(Dictionary<2>);  //<-- 2 arguments
var myClosedListType := typeof(List<String>);
var myListType = typeOf(List<1>); //<-- 1 argument
var myDictionaryType = typeOf(Dictionary<2>);  //<-- 2 arguments
var myClosedListType = typeof(List<String>);
let myListType = typeOf(List<1>) //<-- 1 argument
let myDictionaryType = typeOf(Dictionary<2>)  //<-- 2 arguments
let myClosedListType = typeof(List<String>)
var myListType = typeOf(List<1>); //<-- 1 argument
var myDictionaryType = typeOf(Dictionary<2>);  //<-- 2 arguments
var myClosedListType = typeof(List<String>);

In the above example you see code that gets the type of a List<T> and a Dictionary<TKey,TValue>. List takes one generic argument and Dictionary takes 2. myClosedListType, by contrast, has obtained the concrete `List type.

See Also