IMethod and LuaMethod

There are several types regarding invokable objects in this framework. This section will describe the differences between the types and how they should be used. This only describes how the default environment does it and how it is assumed to work, if you change the environment, it may not be the same. The most likely use of the different types is when you want to accept an argument to a C# defined method and you only want a certain kind of method.


IMethod simply describes an invokable object. This may be a method or delegate, or it can be a user-defined object that supports being invoked. You can implement this interface and it will allow Lua to invoke your object like a method. This interface is the base type of an invokable object. LuaMethod implements this interface, but its use is slightly different. This is intended for an object that supports invoking.


This is the base class of all functions that are used in Lua. There are several different derived classes that are used in different circumstances. This class is meant for an object that is only a function such as a delegate. This should not be used if your objects simply can be invoked. This distinction is not important as the framework simply will call IMethod.Invoke; however other users can make this assumption when accepting arguments.


This class is used when Lua code needs to determine which of several overload choices. This is used both for explicitly registered functions using Lua.Register and for member methods. Internally it stores the MethodInfo and the object target. It then uses reflection to determine which overload is better.


This defines a global method defined in Lua. There is only one for a code file and this is the object returned by the default compiler. Internally, it stores a dynamic type object that defines the global method and creates a new instance each call to Invoke.


This is similar to LuaGlobalMethod except that it is defined for a function nested in code. This is the type of any function that is defined in Lua, even local functions and nested functions. These store an instance of the, global type and the MethodInfo for the generated method for the function.


This is the base type of the framework functions. These are the functions like toStirng and _assert. Each of these functions are defined in the library and are subclasses of this type. This ignores by-reference arguments and overloads.

Defining Your Own Method

If you want to define your own method, you should select the best object to subclass. If it is simply an object that can be invoked, or if you want another base type, you can implement IMethod; otherwise you should derive from LuaMethod. When you do, you need to pass the current environment and a string name to the constructor. The name can be null, it is only used for error messages. Then you need to implement the abstract member InvokeInternal. This is what does the work of invoking your object. The args and byref will never be null. 'overload' may be negative if an overload is not specified. You do not need to use the overload and will most likely ignore it.

Last edited May 26, 2013 at 4:07 AM by ModMaker, version 1


No comments yet.