Beispiel #1
0
		public void TestGenerics ()
		{
			//Im not sure support for generic classes is possible to implement, see: http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.containsgenericparameters.aspx
			//specifically the line that says: "If the ContainsGenericParameters property returns true, the method cannot be invoked"
			//TestClassGeneric<string> genericClass = new TestClassGeneric<string>();
			//lua.RegisterFunction("genericMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("GenericMethod"));
			//lua.RegisterFunction("regularMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("RegularMethod"));
			using (Lua lua = new Lua ()) {
				TestClassWithGenericMethod classWithGenericMethod = new TestClassWithGenericMethod ();

				////////////////////////////////////////////////////////////////////////////
				/// ////////////////////////////////////////////////////////////////////////
				///  IMPORTANT: Use generic method with the type you will call or generic methods will fail with iOS
				/// ////////////////////////////////////////////////////////////////////////
				classWithGenericMethod.GenericMethod<double>(99.0);
				classWithGenericMethod.GenericMethod<TestClass>(new TestClass (99));
				////////////////////////////////////////////////////////////////////////////
				/// ////////////////////////////////////////////////////////////////////////

				lua.RegisterFunction ("genericMethod2", classWithGenericMethod, typeof(TestClassWithGenericMethod).GetMethod ("GenericMethod"));

				try {
					lua.DoString ("genericMethod2(100)");
				} catch {
				}

				Assert.AreEqual (true, classWithGenericMethod.GenericMethodSuccess);
				Assert.AreEqual (true, classWithGenericMethod.Validate<double> (100)); //note the gotcha: numbers are all being passed to generic methods as doubles

				try {
					lua.DoString ("luanet.load_assembly('NLuaTest')");
					lua.DoString ("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
					lua.DoString ("test=TestClass(56)");
					lua.DoString ("genericMethod2(test)");
				} catch {
				}

				Assert.AreEqual (true, classWithGenericMethod.GenericMethodSuccess);
				Assert.AreEqual (56, (classWithGenericMethod.PassedValue as TestClass).val);
			}
		}
        public void TestGenerics()
        {
            Init();

            //Im not sure support for generic classes is possible to implement, see: http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.containsgenericparameters.aspx
            //specifically the line that says: "If the ContainsGenericParameters property returns true, the method cannot be invoked"

            //TestClassGeneric<string> genericClass = new TestClassGeneric<string>();

            //_Lua.RegisterFunction("genericMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("GenericMethod"));
            //_Lua.RegisterFunction("regularMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("RegularMethod"));

            //try
            //{
            //	_Lua.DoString("genericMethod('thestring')");
            //}
            //catch { }

            //try
            //{
            //	_Lua.DoString("regularMethod()");
            //}
            //catch { }

            //if (genericClass.GenericMethodSuccess && genericClass.RegularMethodSuccess && genericClass.Validate("thestring"))
            //	Console.WriteLine("Generic class passed");
            //else
            //	Console.WriteLine("Generic class failed");

            bool passed = true;
            TestClassWithGenericMethod classWithGenericMethod = new TestClassWithGenericMethod();

            _Lua.RegisterFunction("genericMethod2", classWithGenericMethod, typeof(TestClassWithGenericMethod).GetMethod("GenericMethod"));

            try
            {
                _Lua.DoString("genericMethod2(100)");
            }
            catch
            {

            }

            if(!classWithGenericMethod.GenericMethodSuccess || !classWithGenericMethod.Validate<double>(100)) //note the gotcha: numbers are all being passed to generic methods as doubles
                passed = false;

            try
            {
                _Lua.DoString("luanet.load_assembly('TestLua')");
                _Lua.DoString("TestClass=luanet.import_type('LuaInterface.Tests.TestClass')");
                _Lua.DoString("test=TestClass(56)");
                _Lua.DoString("genericMethod2(test)");
            }
            catch
            {

            }

            if(!classWithGenericMethod.GenericMethodSuccess || (classWithGenericMethod.PassedValue as TestClass).val != 56)
                passed = false;

            if(passed)
                Console.WriteLine("Class with generic method passed");
            else
                Console.WriteLine("Class with generic method failed");
        }