Lua處理從C#得到的數組、字典、結構體等

Lua從C#得到到的數組、字典等都是userdata。數組

C#得到到的數組索引從0開始。ui

C#得到到的字典數據處理:this

local testData = CS.LuaCallCSUtils.GetTestData()  --爲一個字典lua

local iter = testData:GetEnumerator()索引

local list = {}string

while iter:MoveNext() doit

    local k = iter.Current.Keyio

    local v = iter.Current.Valuetable

    list[k] = v  --轉爲tablefunction

    print(k, v)

end

C#得到到的數組數據處理:

local testData = CS.LuaCallCSUtils.GetTestData()  --爲一個數組

local iter = testData:GetEnumerator() 

local list = {}

while iter:MoveNext() do

    local v = iter.Current

    table.insert(list, v)

end

C#得到到的結構體處理:

public struct Student  //C#中學生結構體
{
    string name;
    int score;
    public Student(string name, int score)
    {
        this.name = name;
        this.score = score;
    }

    public string GetName()
    {
        return name;
    }

    public int GetScore()
    {
        return score;
    }
}

self = {}  --lua中學生類
self.__index = self

self.name = ""
self.score = 0

function self.new(o)
    o = o or {}
    setmetatable(o, self)
    return o
end

function self:Set(name, score)
    self.name = name
    self.score = score
end

function self:GetName()
    return self.name
end

function self:GetScore()
    return self.score
end

return self

 

local data = CS.LuaCallCSUtils.GetStudent()  --得到一個學生

local stu = require 'student'

local newStu = stu.new()

newStu:Set(data:GetName(), data:GetScore())

print(newStu:GetName())

print(newStu:GetScore())

相關文章
相關標籤/搜索