Skip to content

.NET Types

Kevin Zhao edited this page Jan 25, 2018 · 2 revisions

.NET Types

.NET types can be imported using the ImportType or ImportNamespace functions:

lua.ImportType(typeof(List<>));
lua.ImportNamespace("System.Collections.Generic");

These types will be imported as globals, which you can access using their 'friendly' name. For example, List<> will be imported as List, and Dictionary<,> will be imported as Dictionary. You can then use them transparently like normal .NET types:

lua.ImportType(typeof(Console));
lua.ImportType(typeof(DateTime));
lua.DoString("Console.WriteLine(DateTime.UtcNow)");

Importing .NET Types from Lua

.NET Types can be imported from Lua using the using function:

lua.DoString(@"
    using 'System'
    Console.WriteLine('Hello world!')");

Constructing .NET Objects

You can construct .NET objects by 'calling' the type:

lua.ImportNamespace("System.IO");
lua.DoString("sr = StreamReader('path.txt')");

Constructing Generic Types

You can construct generic types by 'calling' the type with the type arguments. Note that this can be extended indefinitely:

lua.ImportNamespace("System");
lua.ImportNamespace("System.Collections.Generic");
lua.DoString("list = List(List(Dictionary(Int32, Int32)))()");