我試圖將一個Lua錶轉換爲一個C#字節數組。我能得到一個轉換爲雙陣列工作方式如下:將錶轉換爲字節數組
> require 'CLRPackage'
> import "System"
> tbl = {11,22,33,44}
> dbl_arr = Double[4]
> dbl_arr:GetValue(0)
> dbl_arr:GetValue(1)
> for i=0,3 do Console.WriteLine(dbl_arr:GetValue(i)) end
0
0
0
0
> for i,v in ipairs(tbl) do dbl_arr:SetValue(v,i-1) end
> for i=0,3 do Console.WriteLine(dbl_arr:GetValue(i)) end
11
22
33
44
>
但是,如果我改變dbl_arr
到Byte
陣列(dbl_arr = Byte[4]
),然後我得到了以下錯誤:(error object is not a string)
我我試了一堆不同的東西,但沒有運氣。任何幫助,將不勝感激。
更新:
我能夠這樣做是爲了得到錯誤更多的信息:
suc,err = pcall(function() byte_arr:SetValue(12,0) end)
現在suc
是假的,err
返回以下消息:
SetValue failed
System.ArgumentException: Cannot widen from source type to target type either
because the source type is a not a primitive type or the conversion cannot
be accomplished.
at System.Array.InternalSetValue(Void* target, Object value)
at System.Array.SetValue(Object value, Int32 index)
我已經從here安裝了luaforwindows。它的版本是5.1.4-45。我在運行Microsoft Windows XP專業版2002 Service Pack 3的
更新:
這是示例代碼和當錯誤發生時
> require 'CLRPackage'
> import "System"
> tbl = {11,22,33,44}
> dbl_arr = Byte[4]
> for i,v in ipairs(tbl) do dbl_arr:SetValue(v,i-1) end <-- Error occurs here
哪裏'dbl_arr'從何而來? –
@Nicol - 它只是我創建的Double數組:'dbl_arr = Double [4]' – SwDevMan81
不夠公平。我的意思是'Double [4]'來自哪裏。它是否使用某種metatable來使用[]創建對象? –