Exemplo n.º 1
0
        protected static object getNativeValueForLuaValue(Type t, LuaValue luaValue)
        {
            object value = null;

            if (t == typeof(Int32) ||
                t == typeof(Int64) ||
                t == typeof(Int16) ||
                t == typeof(UInt16) ||
                t == typeof(UInt32) ||
                t == typeof(UInt64))
            {
                value = luaValue.toInteger();
            }
            else if (t == typeof(double) ||
                     t == typeof(float))
            {
                value = luaValue.toNumber();
            }
            else if (t == typeof(bool))
            {
                value = luaValue.toBoolean();
            }
            else if (t == typeof(string))
            {
                value = luaValue.toString();
            }
            else if (t.IsArray)
            {
                //数组
                if (t == typeof(byte[]))
                {
                    //二进制数组
                    value = luaValue.toData();
                }
                else if (t == typeof(Int32[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <Int32> intArr = new List <Int32> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(item.toInteger());
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(Int64[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <Int64> intArr = new List <Int64> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(item.toInteger());
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(Int16[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <Int16> intArr = new List <Int16> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToInt16(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(UInt32[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <UInt32> intArr = new List <UInt32> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToUInt32(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(UInt64[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <UInt64> intArr = new List <UInt64> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToUInt64(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(UInt16[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <UInt16> intArr = new List <UInt16> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToUInt16(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(bool[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <bool> boolArr = new List <bool> ();
                        foreach (LuaValue item in arr)
                        {
                            boolArr.Add(Convert.ToBoolean(item.toInteger()));
                        }
                        value = boolArr.ToArray();
                    }
                }
                else if (t == typeof(double[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <double> doubleArr = new List <double> ();
                        foreach (LuaValue item in arr)
                        {
                            doubleArr.Add(item.toNumber());
                        }
                        value = doubleArr.ToArray();
                    }
                }
                else if (t == typeof(float[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <float> floatArr = new List <float> ();
                        foreach (LuaValue item in arr)
                        {
                            floatArr.Add((float)item.toNumber());
                        }
                        value = floatArr.ToArray();
                    }
                }
                else if (t == typeof(string[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <string> floatArr = new List <string> ();
                        foreach (LuaValue item in arr)
                        {
                            floatArr.Add(item.toString());
                        }
                        value = floatArr.ToArray();
                    }
                }
                else
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        ArrayList objArr = new ArrayList();
                        foreach (LuaValue item in arr)
                        {
                            objArr.Add(item.toObject());
                        }
                        value = objArr.ToArray();
                    }
                }
            }
            else if (t == typeof(Hashtable))
            {
                //字典
                Dictionary <string, LuaValue> map = luaValue.toMap();
                if (map != null)
                {
                    if (t == typeof(Dictionary <string, int>))
                    {
                        Dictionary <string, int> dict = new Dictionary <string, int> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toInteger());
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, int>))
                    {
                        Dictionary <string, int> dict = new Dictionary <string, int> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toInteger());
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, uint>))
                    {
                        Dictionary <string, uint> dict = new Dictionary <string, uint> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, Convert.ToUInt32(kv.Value.toInteger()));
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, Int16>))
                    {
                        Dictionary <string, Int16> dict = new Dictionary <string, Int16> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, Convert.ToInt16(kv.Value.toInteger()));
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, UInt16>))
                    {
                        Dictionary <string, UInt16> dict = new Dictionary <string, UInt16> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, Convert.ToUInt16(kv.Value.toInteger()));
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, Int64>))
                    {
                        Dictionary <string, Int64> dict = new Dictionary <string, Int64> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, Convert.ToInt64(kv.Value.toInteger()));
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, UInt64>))
                    {
                        Dictionary <string, UInt64> dict = new Dictionary <string, UInt64> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, Convert.ToUInt64(kv.Value.toInteger()));
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, bool>))
                    {
                        Dictionary <string, bool> dict = new Dictionary <string, bool> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toBoolean());
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, double>))
                    {
                        Dictionary <string, double> dict = new Dictionary <string, double> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toNumber());
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, float>))
                    {
                        Dictionary <string, float> dict = new Dictionary <string, float> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, (float)kv.Value.toNumber());
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, string>))
                    {
                        Dictionary <string, string> dict = new Dictionary <string, string> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toString());
                        }
                        value = dict;
                    }
                    else
                    {
                        Hashtable dict = new Hashtable();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toObject());
                        }
                        value = dict;
                    }
                }
            }
            else
            {
                value = luaValue.toObject();
            }

            return(value);
        }
Exemplo n.º 2
0
        protected static object getNativeValueForLuaValue(Type t, LuaValue luaValue)
        {
            object value = null;

            if (t == typeof(Int32) ||
                t == typeof(Int64) ||
                t == typeof(Int16) ||
                t == typeof(UInt16) ||
                t == typeof(UInt32) ||
                t == typeof(UInt64))
            {
                value = luaValue.toInteger();
            }
            else if (t == typeof(double) ||
                     t == typeof(float))
            {
                value = luaValue.toNumber();
            }
            else if (t == typeof(bool))
            {
                value = luaValue.toBoolean();
            }
            else if (t == typeof(string))
            {
                value = luaValue.toString();
            }
            else if (t.IsArray || typeof(IList).IsAssignableFrom(t))
            {
                //数组
                if (t == typeof(byte[]))
                {
                    //二进制数组
                    value = luaValue.toData();
                }
                else if (t == typeof(Int32[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <Int32> intArr = new List <Int32> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(item.toInteger());
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(Int64[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <Int64> intArr = new List <Int64> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(item.toInteger());
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(Int16[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <Int16> intArr = new List <Int16> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToInt16(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(UInt32[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <UInt32> intArr = new List <UInt32> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToUInt32(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(UInt64[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <UInt64> intArr = new List <UInt64> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToUInt64(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(UInt16[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <UInt16> intArr = new List <UInt16> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToUInt16(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(bool[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <bool> boolArr = new List <bool> ();
                        foreach (LuaValue item in arr)
                        {
                            boolArr.Add(Convert.ToBoolean(item.toInteger()));
                        }
                        value = boolArr.ToArray();
                    }
                }
                else if (t == typeof(double[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <double> doubleArr = new List <double> ();
                        foreach (LuaValue item in arr)
                        {
                            doubleArr.Add(item.toNumber());
                        }
                        value = doubleArr.ToArray();
                    }
                }
                else if (t == typeof(float[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <float> floatArr = new List <float> ();
                        foreach (LuaValue item in arr)
                        {
                            floatArr.Add((float)item.toNumber());
                        }
                        value = floatArr.ToArray();
                    }
                }
                else if (t == typeof(string[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <string> floatArr = new List <string> ();
                        foreach (LuaValue item in arr)
                        {
                            floatArr.Add(item.toString());
                        }
                        value = floatArr.ToArray();
                    }
                }
                else
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        ArrayList objArr = new ArrayList();
                        foreach (LuaValue item in arr)
                        {
                            objArr.Add(item.toObject());
                        }
                        value = objArr.ToArray();
                    }
                }
            }
            else if (typeof(IDictionary).IsAssignableFrom(t))
            {
                //字典
                Dictionary <string, LuaValue> map = luaValue.toMap();
                if (map != null)
                {
                    if (t.IsGenericType)
                    {
                        //为泛型
                        Type[] types     = t.GetGenericArguments();
                        Type   valueType = types [1];

                        IDictionary dict = Activator.CreateInstance(t) as IDictionary;
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, getNativeValueForLuaValue(valueType, kv.Value));
                        }

                        value = dict;
                    }
                    else if (typeof(Hashtable).IsAssignableFrom(t))
                    {
                        Hashtable dict = new Hashtable();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toObject());
                        }
                        value = dict;
                    }
                }
            }
            else
            {
                value = luaValue.toObject();
            }

            return(value);
        }