lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi, Ben,

The table -> interface translation that LuaInterface does was intended to be used to call methods on .NET objects that take an interface (which are quite rare in the base class library, Microsoft uses delegates for everything). luanet.make_object is for subclassing existing classes, not interfaces. The way to convert a Lua table to an interface reference in an embedded scenario is a little convoluted, then:

---- bar.cs
namespace bar {
  public interface IFoo {
    long get_id();
  }
}

---- foo.cs
using bar;
using LuaInterface;
using System;

public class Foo {
  public static void use_foo(IFoo f) {
    Console.WriteLine(f.get_id());
  }

  public static void Main(string[] args) {
    Lua l = new Lua();
    l.RegisterFunction("use_ifoo", null, typeof(Foo).GetMethod("use_foo"));
    l.DoFile("foo.lua");
  }
}

----- foo.lua
 impl = {}
function impl:get_id()
    return 1.0
end
use_ifoo(impl)

------------ end

No need to use make_object, or even load the assembly. I put a feature request at LuaForge so I can remember to add a convenience method to class Lua to do this more easily.

--
Fabio Mascarenhas

On Wed, Jul 2, 2008 at 12:35 PM, Ben Siemon <bsiemon@redbox.com> wrote:

First a summary. We have an interface IFoo. We want to be able to create implementations of this interface from lua code through LuaInterface and then return this implementation created in lua back to the .net code.

 

Now the example code.

 

In the C# file… in an assembly called bar.dll

Namespace bar

{

 

public interface IFoo

{

                long get_id();

}

}

 

In the lua file…

 

luanet.load_assembly("bar")

 

impl = {}

function impl:get_id()

                return 1.0

end

 

function get_instance()

                return make_object(impl, 'bar.IFoo')

end

 

To summarize, we have an interface IFoo that exists in the .net app domain. We want to be able to create new implementations 'on the fly' by evaling the lua file.  A few notes on the lua code. Originally I followed the luainterface paper and tried to call make_object(impl, IFoo) (no quotes) but the LuaInterface could not find the function. Once I added the quotes as I read in the LuaInterfaceTest project it stopped bombing out in the lua interpreter.

 

Now for the use of LuaInterface that is giving me trouble.

 

Lua l = new Lua();

 

l.DoFile(<the above lua code file>);

 

IFoo f = l.GetFunction("get_instance").Call();

 

Instead of getting a new instance of type IFoo f is just null. Based on what I read in the LuaInterface paper this should work however I have not seen this done in any example code so I am still unsure. 

 

 

--

 

time makes fools of us all,

 

ben siemon

 

 

 
 
 
 
 



The information contained in this e-mail and any accompanying documents is intended solely for the person and/or entity to whom it is addressed (i.e. those identified in the "To" and "cc" box) and may contain confidential and/or legally privileged information. Unauthorized disclosure, dissemination, copying or other use of this communication, or any attachment, is strictly prohibited and may be unlawful. If you have received this e-mail in error, please return the e-mail and attachments to the sender and delete the e-mail and attachments and any copy from your system. redbox thanks you for your cooperation.