neolithos / neolua

A Lua implementation for the Dynamic Language Runtime (DLR).
https://neolua.codeplex.com/
Apache License 2.0
467 stars 76 forks source link

Accessing custom generic classes #119

Closed thoj closed 3 years ago

thoj commented 3 years ago

NeoLua Version: 1.3.11

Example to reproduce:

const testNonGen typeof MyNamespace.TestNonGen -- Works fine
const testGen typeof MyNamespace.TestGen[System.Object] -- Parse error
const ListOfObjects typeof System.Collections.Generic.List[System.Object] --Works fine

local test = testNonGen()
local test2 = testGen()
namespace MyNamespace
{
    public class TestGen<T>
    {
        public TestGen () {
            Console.WriteLine("ctor called");
        }

    }

    public class TestNonGen
    {
        public TestNonGen()
        {
            Console.WriteLine("ctor called");
        }

    }
}

So according to the documentation the proper way to reference clr generics is like the above. But only the non generic version works. For the generic version i get this:

Lua Parse Error System.TypeLoadException: Could not load type 'MyNamespace.TestGen`1' from assembly 'Neo.Lua, Version=5.3.0.0, Culture=neutral, PublicKeyToken=fdb0cd4fe8a6e3b2'.
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
   at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
   at System.Type.GetType(String typeName, Boolean throwOnError)

The workaround is this:

((LuaGlobal)global).RegisterPackage("Test", typeof(TestGen<string>));

This works fine. But I can't stop wondering why I can't use the method described in 05_01? This that only for System genereics and not custom ones?

PS. A new nuget release would also be very helpful :)

neolithos commented 3 years ago

I fix it, and pushed a version. Please check it out, and reopen the issue, if something is wrong.

thoj commented 3 years ago

Perfect 👍