Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c# primitive extensions not supported? #520

Open
genis-cats opened this issue Jan 16, 2024 · 1 comment
Open

c# primitive extensions not supported? #520

genis-cats opened this issue Jan 16, 2024 · 1 comment

Comments

@genis-cats
Copy link

genis-cats commented Jan 16, 2024

Hello!

I'd like to make a extension function in primitive type such as string, byte[], byte and so one.

I tested with this code but seems not suported in NLua.

Is there any a way to available access a extension function?


namespace LuaConsoleApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (Lua lua = new Lua())
            {
                lua.State.Encoding = Encoding.UTF8;
                lua["a"] = "a";

                lua.LoadCLRPackage();
                lua.DoString("import('LuaConsoleApp','LuaConsoleApp.StringExtensions')");
                lua.DoString("a:AddString()");
                Console.WriteLine(lua["a"]);
            }
        }
    }
}

namespace LuaConsoleApp.StringExtensions
{
    public static class StringExtensions
    {
        public static string AddString(this string str)
        {
            return str += "a";
        }
    }
}


Exception

NLua.Exceptions.LuaScriptException: '[string "chunk"]:1: attempt to call a nil value (method 'AddString')'
@viniciusjarina
Copy link
Member

It's because a is a Lua string and not C# string. Try a = StringExtensions.AddString(a) and see if it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants