示例#1
0
        /// <summary>
        /// Converts a table to a <see cref="List{T}"/>
        /// </summary>
        internal static object ConvertTableToListOfGenericType(Type listType, Type itemType, Table table)
        {
            if (listType.GetGenericTypeDefinition() != typeof(List <>))
            {
                listType = typeof(List <>);
                listType = listType.MakeGenericType(itemType);
            }

            System.Collections.IList lst = (System.Collections.IList)Activator.CreateInstance(listType);

            for (int i = 1, l = table.Length; i <= l; i++)
            {
                DynValue v = table.Get(i);
                object   o = ScriptToClrConversions.DynValueToObjectOfType(v, itemType, null, false);
                lst.Add(o);
            }

            return(lst);
        }
示例#2
0
        /// <summary>
        /// Converts a table to a T[]
        /// </summary>
        internal static object ConvertTableToArrayOfGenericType(Type arrayType, Type itemType, Table table)
        {
            List <object> lst = new List <object>();

            for (int i = 1, l = table.Length; i <= l; i++)
            {
                DynValue v = table.Get(i);
                object   o = ScriptToClrConversions.DynValueToObjectOfType(v, itemType, null, false);
                lst.Add(o);
            }

            System.Collections.IList array = (System.Collections.IList)Activator.CreateInstance(arrayType, new object[] { lst.Count });

            for (int i = 0; i < lst.Count; i++)
            {
                array[i] = lst[i];
            }

            return(array);
        }
示例#3
0
        /// <summary>
        /// Converts a table to a <see cref="Dictionary{K,V}"/>
        /// </summary>
        internal static object ConvertTableToDictionaryOfGenericType(Type dictionaryType, Type keyType, Type valueType, Table table)
        {
            if (dictionaryType.GetGenericTypeDefinition() != typeof(Dictionary <,>))
            {
                dictionaryType = typeof(Dictionary <,>);
                dictionaryType = dictionaryType.MakeGenericType(keyType, valueType);
            }

            System.Collections.IDictionary dic = (System.Collections.IDictionary)Activator.CreateInstance(dictionaryType);

            foreach (var kvp in table.Pairs)
            {
                object key = ScriptToClrConversions.DynValueToObjectOfType(kvp.Key, keyType, null, false);
                object val = ScriptToClrConversions.DynValueToObjectOfType(kvp.Value, valueType, null, false);

                dic.Add(key, val);
            }

            return(dic);
        }