Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

parsing - Easiest way to parse a Lua datastructure in C# / .Net

Anyone know of an easy way to parse a Lua datastructure in C# or with any .Net library? This would be similar to JSON decoding, except for Lua instead of javascript.

At this point it looks like I'll need to write my own, but hoping there's something already out there.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks to both of you, I found what I was looking for using LuaInterface

Here's a datastructure in Lua I wanted to read ("c:sample.lua"):

TestValues = {
    NumbericOneMillionth = 1e-006,
    NumbericOnehalf = 0.5,
    NumbericOne = 1,
    AString = "a string"
}

Here's some sample code reading that Lua datastructure using LuaInterface:

Lua lua = new Lua();

var result = lua.DoFile("C:\sample.lua");

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

And here's what that sample code writes to the console:

(System.String) AString = a string
(System.Double) NumbericOneMillionth = 1E-06
(System.Double) NumbericOnehalf = 0.5
(System.Double) NumbericOne = 1

To figure out how to use the library I opened up the LuaInterface.dll in Reflector and google'd the member functions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...