### Load CLR Package and Import .NET Assemblies in Lua Source: https://nlua.org Enables NLua to interact with the Common Language Runtime (CLR) and allows Lua scripts to import .NET assemblies using the 'import' function. This example imports 'System.Web' and a custom assembly 'MyAssembly' with namespace 'MyNamespace'. ```csharp state.LoadCLRPackage (); state.DoString (@" import ('MyAssembly', 'MyNamespace') import ('System.Web') "); ``` -------------------------------- ### Define a C# Class for NLua Source: https://nlua.org Example of a C# class that can be used with NLua. NLua allows direct instantiation and usage of .NET classes without prior registration. ```csharp public class SomeClass { public string MyProperty {get; private set;} public SomeClass (string param1 = "defaulValue") { MyProperty = param1; } public int Func1 () { return 32; } public string AnotherFunc (int val1, string val2) { return "Some String"; } public static string StaticMethod (int param) { return "Return of Static Method"; } } ``` -------------------------------- ### Access .NET Properties from Lua Source: https://nlua.org Illustrates how to get the value of a public property ('MyProperty') of a .NET object from Lua using the '.' notation. ```csharp state.DoString (@" local res5 = obj.MyProperty "); ``` -------------------------------- ### Build NLua Project Source: https://nlua.org Command to build the NLua project using msbuild. ```bash msbuild ``` -------------------------------- ### Call .NET Instance Methods from Lua Source: https://nlua.org Shows how to invoke instance methods of .NET objects from Lua using the ':' notation. This includes calling methods on objects passed from C# ('obj') and objects created within Lua ('obj2', 'client'). ```csharp state.DoString (@" local res1 = obj:Func1() local res2 = obj2:AnotherFunc (10, 'hello') local res3 = client:DownloadString('http://nlua.org') "); ``` -------------------------------- ### Create .NET Objects within Lua Source: https://nlua.org Demonstrates creating instances of .NET classes directly within a Lua script using NLua. 'SomeClass' is instantiated with default values, and 'WebClient' is created to download content. ```csharp state.DoString (@" obj2 = SomeClass() -- you can suppress default values. client = WebClient() "); ``` -------------------------------- ### Create Lua State in C# Source: https://nlua.org Instantiate a new Lua state object in C# using NLua. ```csharp using NLua; Lua state = new Lua () ``` -------------------------------- ### Call .NET Static Methods from Lua Source: https://nlua.org Demonstrates calling a static method of a .NET class ('SomeClass') from Lua using the '.' notation. ```csharp state.DoString (@" local res4 = SomeClass.StaticMethod(4) "); ``` -------------------------------- ### Implement Lua Sandboxing Source: https://nlua.org A simple method to sandbox Lua scripts by redefining the 'import' function to do nothing, preventing users from importing .NET assemblies. ```csharp state.DoString (@" import = function () end "); ``` -------------------------------- ### Evaluate Simple Lua Expressions Source: https://nlua.org Execute a simple Lua arithmetic expression from C# and retrieve the result. DoString returns an array of objects as Lua functions can return multiple values. ```csharp var res = state.DoString ("return 10 + 3*(5 + 2)")[0] as double; ``` -------------------------------- ### Pass .NET Objects to Lua State Source: https://nlua.org Create an instance of a C# class 'SomeClass' and assign it to a Lua global variable named 'obj'. This allows Lua scripts to interact with the .NET object. ```csharp 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 ``` -------------------------------- ### Retrieve Global Values from Lua State Source: https://nlua.org Execute a Lua statement to create a global variable 'y' and then retrieve its value back into C#. ```csharp state.DoString ("y = 10 + x*(5 + 2)"); var y = state ["y"] as double; // Retrieve the value of y ``` -------------------------------- ### Retrieve and Call Lua Functions Source: https://nlua.org Define a Lua function 'ScriptFunc' and retrieve it as a LuaFunction object in C#. The function is then called with arguments, and its first return value is cast to an integer. ```csharp state.DoString (@" function ScriptFunc (val1, val2) if val1 > val2 then return val1 + 1 else return val2 - 1 end end "); var scriptFunc = state ["ScriptFunc"] as LuaFunction; var res = (int)scriptFunc.Call (3, 5).First (); ``` -------------------------------- ### Pass Raw Values to Lua State Source: https://nlua.org Assign a C# double value to a Lua global variable named 'x'. ```csharp double val = 12.0; state ["x"] = val; // Create a global value 'x' var res = state.DoString ("return 10 + x*(5 + 2)")[0] as double; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.