1

I am trying to iterate through LuaTable object in C#, but getting error.

my lua file is:

config = {}
config.visibility = 0

and my C# code:

LuaTable config = lua.GetTable("config");
Console.WriteLine(config["visibility"].ToString());
foreach (DictionaryEntry member in config)
{
    Console.WriteLine("({0}) {1} = {2}",
        member.Value.GetType().ToString(),
        member.Key,
        member.Value);
}

which produces this output:

0

Unhandled Exception: System.InvalidCastException: Cannot cast from source type to destination type.

If I ask just fot value at key visibility, I get correct answer, but I am unable to iterate through keys and values.

Which class should I use instead of DictionaryEntry?

Thanks, Zbynek

Zbynek
  • 5,673
  • 6
  • 30
  • 52
  • the exception occurs during the loop, first time through, right? Which one of the 3 member lines raises the exception? – Oliver Jan 08 '14 at 18:29
  • error is raised by line with `foreach` – Zbynek Jan 08 '14 at 19:30
  • So compiler is unable to get the dictionary enumerator from config. Does config (ie LuaTable) inherit from IDictionary, and implement the GetEnumerator? – Oliver Jan 08 '14 at 20:22
  • It does implement `IDictionaryEnumerator`: [https://github.com/NLua/NLua/blob/master/Core/NLua/LuaTable.cs](https://github.com/NLua/NLua/blob/master/Core/NLua/LuaTable.cs). – Zbynek Jan 08 '14 at 20:43

2 Answers2

2

Well, solution found - following code works:

LuaTable tb = lua.GetTable("config");

Dictionary<object, object> dict = lua.GetTableDict(tb);

foreach (KeyValuePair<object, object> de in dict)
{
    Console.WriteLine("{0} {1}", de.Key.ToString(), de.Value.ToString());
}

I still don't know, why iterating through LuaTable did not work, so I will leave this question open. Also - If I set types of key and value to something different than object (e.g. string and int), it produces Cannot convert type 'System.Collections.Generic.KeyValuePair<string,int>' to 'System.Collections.Generic.KeyValuePair<object,object>' error.

So for now, I leave this as a workaround and any suggestions will be still welcome.

Zbynek
  • 5,673
  • 6
  • 30
  • 52
  • You can probably also skip the `dict` object (at least `for each (KeyValuePair ^d in tb)` worked for me in C++/CLI). – Avi Ginsburg Mar 28 '17 at 08:18
0

Although LuaTable has GetEnumerator/Keys/Values methods, it doesn't inherit from IDictionary. The following might still be applicable: http://lua-users.org/lists/lua-l/2005-01/msg00662.html. See How to use foreach keyword on custom Objects in C# as well. You might have to extend LuaTable or, worse, patch it.

Community
  • 1
  • 1
Oliver
  • 27,510
  • 9
  • 72
  • 103
  • just updating: `LuaTable` implements `IDictionaryEnumerator` and following works ok: `config.GetEnumerator();` For some reason, however, foreach loop fails – Zbynek Jan 08 '14 at 20:46
  • @Zbynek Just because it defines the same method isn't sufficient for use with `foreach`. Have you looked at the "how-to" link above? – Oliver Jan 09 '14 at 15:57