LuaInterface: Call System.Encoding.UTF8.GetBytes from Lua Script

490 views Asked by At

I am using LuaInterface to use .NET types in Lua. I wanted to showcase this ability by using the Cryptography classes from .NET. However, in order to pass a string to an RSACryptoProvider, I need to convert it to an array of bytes beforehand. This can normally be done using System.Encoding.UTF8.GetBytes. However, trying to call this function yields an error:

> 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]: ?

What is the proper way to call the GetBytes function? Am I missing something?

1

There are 1 answers

0
Tom Blodget On BEST ANSWER

UTF8 is a static property of the class System.Text.Encoding.

Also, GetBytes is an instance method so call it with Lua's method call syntax.

So:

bytes = luanet.System.Text.Encoding.UTF8:GetBytes("foobar")