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, null); // 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, null); // 3
Dim stringTypeRef = RemObjects.Elements.System.typeOf(String)
Dim lengthProperty = stringTypeRef.GetProperty("Length")

Dim someString = "abc"
Dim len = lengthProperty.GetValue(someString, Nothing) ' 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>);
Dim myListType = RemObjects.Elements.System.typeOf(List(Of 1)) // <-- 1 argument
Dim myDictionaryType = RemObjects.Elements.System.typeOf(Dictionary(Of 2))  // <-- 2 arguments
Dim myClosedListType = RemObjects.Elements.System.typeOf(List(Of 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<String> type.

Note for Mercury

Since Mercury has a TypeOf keyword, that keyword hides the typeOf() System function from scope. In order to use it in a .vb file, it will always need to be prefixed with the full namespace, RemObjects.Elements.System.typeOf().

See Also