lua-users home
lua-l archive

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


Hi, Dan,

I am trying to understand your question... LuaInterface does not store
types of CLR objects: everything is an Object to it, and it uses the
type information of the object itself (GetType). This information is
never changed by casting. When you pass the object as an argument to a
method LuaInterface will see if the object's type is compatible with
the parameter's type, and CLR reflection will do the casting behind
the scenes when LuaInterface invokes the method. Same thing if you try
to store the object in a field. No up or downcasting is ever
necessary. Check the samples for some examples of this.

--
Fabio Mascarenhas

On 8/4/06, Dan Hubbert <dan.hubbert@gmail.com> wrote:

Hi list,

I realise that asking a casting question in a dynamically typed language is
odd, but I'm hoping this will make sense.

I'm using Lua 5.1, .Net 2.0 and the LuaInterface to build a tool to automate
actions in Microsoft Content Management Server 2002 using Lua scripts.
Mostly the Lua scripts will call utility functions I've written in C#, but
occasionally it would be easier to call a native function from the Lua
script, and this is where I get casting problems.

For example MCMS publishing API provides a function 'GetByPath' that returns
a HierarchyItem if you provide the Path of an MCMS object. HierarchyItem is
the base class for many more specific classes like Posting, Channel etc and
the function could return any of those more speficic types.

There are many functions which require a Posting or Channel as a parameter
though. If you call GetByPath from Lua the HierarchyItem gets turned into a
UserData object, and when you pass it back to C# it will be a HierarchyItem
again. Is there any way to change that? Can you cast a parameter before it
gets turned into a UserData object, so that it is a more specific type when
it gets passed back to C#?

I've written various C# wrapper functions at the moment which do the casting
for me, but I'd like to distribute this tool to our systems team and have
them only editing scripts. If these wrapper functions are really necessary I
can see them having to make more and uses for these scripts evolve.

Thanks,


Dan Hubbert