Using the .NET objects.

SomeClass obj = new SomeClass ("Param");
state ["obj"] = obj; // Create a global value 'obj' of .NET type SomeClass // This could be any .NET object, from BCL or from your assemblies

Using .NET assemblies inside Lua:

To access any .NET assembly to create objects, events etc inside Lua you need to ask NLua to use CLR as a Lua package.
To do this just use the method LoadCLRPackage and use the import function inside your Lua script to load the Assembly.

state.LoadCLRPackage ();
state.DoString (@" import ('MyAssembly.exe', 'MyNamespace') import ('System.Web') ");
// import will load any .NET assembly and they will be available inside the Lua context.

Creating .NET objects:
To create object you only need to use the class name with the ().

Calling static methods:
You can call static methods using only the class name and the . notation from Lua.

state.DoString (@" local res4 = SomeClass.StaticMethod(4)");

Calling properties:
You can get (or set) any property using . notation from Lua.

state.DoString (@" local res5 = obj.MyProperty");

All methods, events or property need to be public available, NLua will fail to call non-public members.

If you are using Xamarin.iOS you need to Preserve the class you want to use inside NLua, otherwise the Linker will remove the class from final binary if the class is not in use.

Sandboxing

There is many ways to sandbox scripts inside your application. I strongly recomend you to use plain Lua to do your sandbox.
You can re-write the import function before load the user script and if the user try to import a .NET assembly nothing will happen.

Old History

LuaInterface

Getting started with NLua:

Look at src/TestNLua/TestLua to see an example of usage from C#
(optionally you can run this from inside the NLua solution using the debugger).
Also provides a good example of how to override .NET methods of Lua and usage of NLua
from within your .NET application.

Look at samples/testluaform.lua to see examples of how to use
.NET inside Lua

More installation and usage instructions in the doc/guide.pdf file.

What's new in LuaInterface 2.0.3

Fix: Private methods accessible via LuaInterface

Fix: Method overload lookup failures

Fix: Lua DoFile memory leaks when file not found (submitted by Paul Moore)

What's new in LuaInterface 1.5.1

Fix a serious bug w.r.t. garbage collection - made especially apparent
with the new lua5.1 switch: If you were very unlucky with timing
sometimes Lua would loose track of pointers to CLR functions.

When I added support for static methods, I allowed the user to use either a
colon or a dot to separate the method from the class name. This was not
correct - it broke disambiguation between overloaded static methods.
Therefore, LuaInterface is now more strict: If you want to call a static
method, you must use dot to separate the method name from the class name. Of
course you can still use a colon if an instance is being used.

Static method calls are now much faster (due to better caching).

What's new in LuaInterface 1.5

LuaInterface is now updated to be based on Lua5.1.1. You can either use
your own build/binaries for Lua5.1.1 or use the version distributed here.
(Lots of thanks to Steffen Itterheim for this work!)

LuaInterface.Lua no longer has OpenLibs etc... The base mechanism for
library loading for Lua has changed, and we haven't yet broken appart
the library loading for LuaInterface. Instead, all standard Lua libraries
are automatically loaded at start up.

Fixed a bug where calls of some static methods would reference an
invalid pointer.

Fixed a bug when strings with embedded null characters are passed in or
out of Lua (Thanks to Daniel N�ri for the report & fix!)

The native components in LuaInterface (i.e. Lua51 and the loader) are
both built as release builds - to prevent problems loading standard
windows libraries.

Note: You do not need to download/build lua-5.1.1.zip unless you want to
modify Lua internals (a built version of lua51.dll is included in the
regular LuaInterface distribution)

What's New in LuaInterface 1.4

Note: Fabio area of interest has moved off in other directions (hopefully only temporarily).
I've talked with Fabio and he's said he's okay with me doing a new release with various fixes
I've made over the last few months. Changes since 1.3:

Visual Studio 2005/.Net 2.0 is supported.

Compat-5.1 is modified to expect backslash as the path seperator.

LuaInterface will now work correctly with Generic C# classes.

CLR inner types are now supported.

Fixed a problem where sometimes Lua proxy objects would be associated with the wrong CLR object.

If a CLR class has an array accessor, the elements can be accessed using the regular Lua indexing
interface.

Add CLRPackage.lua to the samples directory. This class makes it much easier to automatically
load referenced assemblies. In the next release this loading will be automatic.

To see an quick demonstration of LuaInterface, cd into nlua/samples and then
type: ....\Built\debug\LuaRunner.exe testluaform.lua

Various other minor fixes that I've forgotten. I'll keep better track next time.

Note: LuaInterface is still based on Lua 5.0.2. If someone really wants us to upgrade to Lua 5.1
please send me a note. In the mean time, I'm also distributing a version of
Lua 5.0.2 with an appropriate VS 2005 project file. You do not need to
download this file unless you want to modify Lua internals (a built version
of lua50.dll is included in the regular LuaInterface distribution)

LuaInterface.dll now has a strong name, and can be put in the GAC (thanks to Ivan Voras).

You can now use foreach with instances of LuaTable (thanks to Zachary Landau).

There is an alternate form of loading assemblies and importing types (based on an
anonymous contribution in the Lua wiki). Check the _alt files in the samples folder.

What's New in LuaInterface 1.2.1

Now checks if two LuaInterface.Lua instances are trying to share the same Lua state,
and throws an exception if this is the case. Also included readonly clauses in public
members of the Lua and ObjectTranslator classes.

This version includes the source of LuaInterfaceLoader.dll, with VS.Net 2003 project
files.

What's New in LuaInterface 1.2

LuaInterface now can be loaded as a module, so you can use the lua standalone
interpreter to run scripts. Thanks to Paul Winwood for this idea and sample code
showing how to load the CLR from a C++ program. The module is "nlua". Make
sure Lua can find nlua.lua, and LuaInterfaceLoader.dll is either in the
current directory or the GAC. The samples now load LuaInterface as a module, in
its own namespace.

The get_method_bysig, get_constructor_bysig and make_object were changed: now you
pass the names of the types to them, instead of the types themselves. E.g: