LuaInterface:从Lua脚本调用System.Encoding.UTF8.GetBytes函数。

我正在使用LuaInterface在Lua中使用.NET类型。我想展示这种能力,通过使用.NET中的加密类。然而,为了将字符串传递给_RSACryptoProvider_,我需要先将其转换为字节数组。这通常可以使用_System.Encoding.UTF8.GetBytes_来完成。然而,尝试调用此函数会产生错误:

> bytes = luanet.System.Encoding.UTF8.GetBytes("foobar")
stdin:1: No such type: System.Encoding.UTF8.GetBytes
stack traceback:
        [C]: in function 'error'
        [string "local metatable = {}
                        ..."]:30: in function 'GetBytes'
        stdin:1: in main chunk
        [C]: ?

调用GetBytes函数的正确方法是什么?我有什么遗漏的吗?

点赞
用户2226988
用户2226988

UTF8 是 System.Text.Encoding 类的静态属性。

同时,GetBytes 是一个实例方法,因此需要使用 Lua 的方法调用语法来调用它。

因此,代码如下:

bytes = luanet.System.Text.Encoding.UTF8:GetBytes("foobar")
2014-08-16 16:55:01